target/riscv: Fix PMP related problem
cover target/riscv: Fix PMP related problem
This patchset tries to fix the PMP bypass problem issue https://gitlab.com/qemu-project/qemu/-/issues/1542
The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-pmp-fix
Weiwei Li (6):
target/riscv: Update pmp_get_tlb_size()
target/riscv: Move pmp_get_tlb_size apart from
get_physical_address_pmp
target/riscv: flush tlb when pmpaddr is updated target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes target/riscv: flush tb when PMP entry changes accel/tcg: Remain TLB_INVALID_MASK in the address when TLB is
re-filled
accel/tcg/cputlb.c | 7 ----- target/riscv/cpu_helper.c | 19 ++++--------- target/riscv/pmp.c | 60 ++++++++++++++++++++++++++------------- target/riscv/pmp.h | 3 +- 4 files changed, 47 insertions(+), 42 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/6 target/riscv: Update pmp_get_tlb_size()
Not only the matched PMP entry, Any PMP entry that overlap with partial of the tlb page may make the regions in that page have different permission rights. So all of them should be taken into consideration.
--- target/riscv/cpu_helper.c | 7 ++----- target/riscv/pmp.c | 34 +++++++++++++++++++++------------- target/riscv/pmp.h | 3 +-- 3 files changed, 24 insertions(+), 20 deletions(-)
target/riscv/cpu_helper.c +2 -5
@@ -703,11 +703,8 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, } *prot =pmp_priv_to_page_prot (pmp_priv); -if ((tlb_size !=NULL ) && pmp_index !=MAX_RISCV_PMPS ) { -target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE -1 ); -target_ulong tlb_ea = tlb_sa +TARGET_PAGE_SIZE -1 ; - - *tlb_size =pmp_get_tlb_size (env, pmp_index, tlb_sa, tlb_ea); +if (tlb_size !=NULL ) { + *tlb_size =pmp_get_tlb_size (env, addr); }return TRANSLATE_SUCCESS ;
target/riscv/pmp.c +21 -13
@@ -601,28 +601,36 @@ target_ulong mseccfg_csr_read(CPURISCVState *env) }/* -* Calculate the TLB size if the start address or the end address of +* Calculate the TLB size if any start address or the end address of * PMP entry is presented in the TLB page. */ -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, -target_ulong tlb_sa,target_ulong tlb_ea) +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr) { -target_ulong pmp_sa = env->pmp_state.addr[pmp_index].sa; -target_ulong pmp_ea = env->pmp_state.addr[pmp_index].ea; +target_ulong pmp_sa; +target_ulong pmp_ea; +target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE -1 ); +target_ulong tlb_ea = tlb_sa +TARGET_PAGE_SIZE -1 ; +int i; + +for (i =0 ; i <MAX_RISCV_PMPS ; i++) { + pmp_sa = env->pmp_state.addr[i].sa; + pmp_ea = env->pmp_state.addr[i].ea; -if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) { -return TARGET_PAGE_SIZE ; - }else {/* -* At this point we have a tlb_size that is the smallest possible size -* That fits within a TARGET_PAGE_SIZE and the PMP region. -* -* If the size is less then TARGET_PAGE_SIZE we drop the size to 1. +* If any start address or the end address of PMP entry is presented +* in the TLB page and cannot override the whole TLB page we drop the +* size to 1. * This means the result isn't cached in the TLB and is only used for * a single translation. */ -return 1 ; +if (((pmp_sa >= tlb_sa && pmp_sa <= tlb_ea) || + (pmp_ea >= tlb_sa && pmp_ea <= tlb_ea)) && + !(pmp_sa == tlb_sa && pmp_ea == tlb_ea)) { +return 1 ; + } } + +return TARGET_PAGE_SIZE ; }/*
target/riscv/pmp.h +1 -2
@@ -76,8 +76,7 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, target_ulong size, pmp_priv_t privs, pmp_priv_t *allowed_privs, target_ulong mode); -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, -target_ulong tlb_sa,target_ulong tlb_ea); +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr);void pmp_update_rule_addr (CPURISCVState *env,uint32_t pmp_index);void pmp_update_rule_nums (CPURISCVState *env);uint32_t pmp_get_num_rules (CPURISCVState *env);
Signature · 1 line
2.25.1
Re: [PATCH 1/6] target/riscv: Update pmp_get_tlb_size()
Quoted 55 lines · Weiwei Li
Not only the matched PMP entry, Any PMP entry that overlap with partial of the tlb page may make the regions in that page have different permission rights. So all of them should be taken into consideration.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>--- target/riscv/cpu_helper.c | 7 ++----- target/riscv/pmp.c | 34 +++++++++++++++++++++------------- target/riscv/pmp.h | 3 +-- 3 files changed, 24 insertions(+), 20 deletions(-)target/riscv/cpu_helper.c +2
-5@@ -703,11 +703,8 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, } *prot =pmp_priv_to_page_prot (pmp_priv); -if ((tlb_size !=NULL ) && pmp_index !=MAX_RISCV_PMPS ) { -target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE -1 ); -target_ulong tlb_ea = tlb_sa +TARGET_PAGE_SIZE -1 ; - - *tlb_size =pmp_get_tlb_size (env, pmp_index, tlb_sa, tlb_ea); +if (tlb_size !=NULL ) { + *tlb_size =pmp_get_tlb_size (env, addr); }return TRANSLATE_SUCCESS ;target/riscv/pmp.c +8
-5@@ -601,28 +601,36 @@ target_ulong mseccfg_csr_read(CPURISCVState *env) }/* -* Calculate the TLB size if the start address or the end address of +* Calculate the TLB size if any start address or the end address of * PMP entry is presented in the TLB page. */ -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, -target_ulong tlb_sa,target_ulong tlb_ea) +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr) { -target_ulong pmp_sa = env->pmp_state.addr[pmp_index].sa; -target_ulong pmp_ea = env->pmp_state.addr[pmp_index].ea; +target_ulong pmp_sa; +target_ulong pmp_ea; +target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE -1 ); +target_ulong tlb_ea = tlb_sa +TARGET_PAGE_SIZE -1 ; +int i; +
target/riscv/pmp.c target_ulong mseccfg_csr_read(CPURISCVState *env) · line 618
Quoted 10 lines
target/riscv/pmp.c +3
-5+for (i =0 ; i <MAX_RISCV_PMPS ; i++) { + pmp_sa = env->pmp_state.addr[i].sa; + pmp_ea = env->pmp_state.addr[i].ea; -if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) { -return TARGET_PAGE_SIZE ; - }else {/* -* At this point we have a tlb_size that is the smallest possible size -* That fits within a TARGET_PAGE_SIZE and the PMP region.
This comment points out that we should have the smallest region, so I'm not clear why we need this change. Can you update the commit description to be clear on why this change is needed and what it fixes?
Alistair
Quoted 38 lines
target/riscv/pmp.c +10
-3-* -* If the size is less then TARGET_PAGE_SIZE we drop the size to 1. +* If any start address or the end address of PMP entry is presented +* in the TLB page and cannot override the whole TLB page we drop the +* size to 1. * This means the result isn't cached in the TLB and is only used for * a single translation. */ -return 1 ; +if (((pmp_sa >= tlb_sa && pmp_sa <= tlb_ea) || + (pmp_ea >= tlb_sa && pmp_ea <= tlb_ea)) && + !(pmp_sa == tlb_sa && pmp_ea == tlb_ea)) { +return 1 ; + } } + +return TARGET_PAGE_SIZE ; }/* target/riscv/pmp.h +1
-3@@ -76,8 +76,7 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, target_ulong size, pmp_priv_t privs, pmp_priv_t *allowed_privs, target_ulong mode); -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, -target_ulong tlb_sa,target_ulong tlb_ea); +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr);void pmp_update_rule_addr (CPURISCVState *env,uint32_t pmp_index);void pmp_update_rule_nums (CPURISCVState *env);uint32_t pmp_get_num_rules (CPURISCVState *env); --2.25.1
Re: [PATCH 1/6] target/riscv: Update pmp_get_tlb_size()
Quoted 59 lines · Alistair Francis
Quoted 58 lines · Weiwei Li
Not only the matched PMP entry, Any PMP entry that overlap with partial of the tlb page may make the regions in that page have different permission rights. So all of them should be taken into consideration.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>--- target/riscv/cpu_helper.c | 7 ++----- target/riscv/pmp.c | 34 +++++++++++++++++++++------------- target/riscv/pmp.h | 3 +-- 3 files changed, 24 insertions(+), 20 deletions(-)target/riscv/cpu_helper.c +2
-5@@ -703,11 +703,8 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, } *prot =pmp_priv_to_page_prot (pmp_priv); -if ((tlb_size !=NULL ) && pmp_index !=MAX_RISCV_PMPS ) { -target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE -1 ); -target_ulong tlb_ea = tlb_sa +TARGET_PAGE_SIZE -1 ; - - *tlb_size =pmp_get_tlb_size (env, pmp_index, tlb_sa, tlb_ea); +if (tlb_size !=NULL ) { + *tlb_size =pmp_get_tlb_size (env, addr); }return TRANSLATE_SUCCESS ;target/riscv/pmp.c +11
-5@@ -601,28 +601,36 @@ target_ulong mseccfg_csr_read(CPURISCVState *env) }/* -* Calculate the TLB size if the start address or the end address of +* Calculate the TLB size if any start address or the end address of * PMP entry is presented in the TLB page. */ -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, -target_ulong tlb_sa,target_ulong tlb_ea) +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr) { -target_ulong pmp_sa = env->pmp_state.addr[pmp_index].sa; -target_ulong pmp_ea = env->pmp_state.addr[pmp_index].ea; +target_ulong pmp_sa; +target_ulong pmp_ea; +target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE -1 ); +target_ulong tlb_ea = tlb_sa +TARGET_PAGE_SIZE -1 ; +int i; + +for (i =0 ; i <MAX_RISCV_PMPS ; i++) { + pmp_sa = env->pmp_state.addr[i].sa; + pmp_ea = env->pmp_state.addr[i].ea;
Quoted 10 lines
target/riscv/pmp.c target_ulong mseccfg_csr_read(CPURISCVState *env) · line 618
Quoted 6 lines
target/riscv/pmp.c +0
-5-if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) { -return TARGET_PAGE_SIZE ; - }else {/* -* At this point we have a tlb_size that is the smallest possible size -* That fits within a TARGET_PAGE_SIZE and the PMP region. This comment points out that we should have the smallest region, so I'm not clear why we need this change. Can you update the commit description to be clear on why this change is needed and what it fixes?
This function return tlb_size to 1 to make the tlb uncached. However, In previous implementation,
only the matched PMP entry of current access address is taken into consideration. Then, if other PMP entry
that match other address in the same page, we may also cannot cache the tlb, since different address
in that page may have different permission rights.
Regards,
Weiwei Li
Quoted 40 lines
Alistair
Quoted 38 lines
target/riscv/pmp.c +10
-3-* -* If the size is less then TARGET_PAGE_SIZE we drop the size to 1. +* If any start address or the end address of PMP entry is presented +* in the TLB page and cannot override the whole TLB page we drop the +* size to 1. * This means the result isn't cached in the TLB and is only used for * a single translation. */ -return 1 ; +if (((pmp_sa >= tlb_sa && pmp_sa <= tlb_ea) || + (pmp_ea >= tlb_sa && pmp_ea <= tlb_ea)) && + !(pmp_sa == tlb_sa && pmp_ea == tlb_ea)) { +return 1 ; + } } + +return TARGET_PAGE_SIZE ; }/* target/riscv/pmp.h +1
-3@@ -76,8 +76,7 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr, target_ulong size, pmp_priv_t privs, pmp_priv_t *allowed_privs, target_ulong mode); -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, -target_ulong tlb_sa,target_ulong tlb_ea); +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr);void pmp_update_rule_addr (CPURISCVState *env,uint32_t pmp_index);void pmp_update_rule_nums (CPURISCVState *env);uint32_t pmp_get_num_rules (CPURISCVState *env); --2.25.1
Re: [PATCH 1/6] target/riscv: Update pmp_get_tlb_size()
Quoted 75 lines · Weiwei Li
Quoted 61 lines · Alistair Francis
Quoted 60 lines · Weiwei Li
Not only the matched PMP entry, Any PMP entry that overlap with partial of the tlb page may make the regions in that page have different permission rights. So all of them should be taken into consideration.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
target/riscv/cpu_helper.c| 7 ++----- target/riscv/pmp.c | 34 +++++++++++++++++++++------------- target/riscv/pmp.h | 3 +--3 files changed, 24 insertions(+), 20 deletions(-)target/riscv/cpu_helper.c +2
-5@@ -703,11 +703,8 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, } *prot = pmp_priv_to_page_prot(pmp_priv); - if ((tlb_size != NULL) && pmp_index != MAX_RISCV_PMPS) { - target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1); - target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1; - - *tlb_size = pmp_get_tlb_size(env, pmp_index, tlb_sa, tlb_ea); + if (tlb_size != NULL) { + *tlb_size = pmp_get_tlb_size(env, addr); }return
TRANSLATE_SUCCESS;target/riscv/pmp.c +11
-5@@ -601,28 +601,36 @@ target_ulong mseccfg_csr_read(CPURISCVState *env) } /* -* Calculate the TLB size if the start address or the end address of +* Calculate the TLB size if any start address or the end address of * PMP entry is presented in the TLB page. */ -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, - target_ulong tlb_sa, target_ulong tlb_ea) +target_ulong pmp_get_tlb_size(CPURISCVState *env, target_ulong addr) { - target_ulong pmp_sa = env->pmp_state.addr[pmp_index].sa; - target_ulong pmp_ea = env->pmp_state.addr[pmp_index].ea; + target_ulong pmp_sa; + target_ulong pmp_ea; + target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1); + target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1; + int i; + + for (i = 0; i < MAX_RISCV_PMPS; i++) { + pmp_sa = env->pmp_state.addr[i].sa; + pmp_ea = env->pmp_state.addr[i].ea;Quoted 11 lines
Quoted 7 lines
target/riscv/pmp.c +0
-4- if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) { - return TARGET_PAGE_SIZE; - } else { /* -* At this point we have a tlb_size that is the smallest possible size
- * That fits within aTARGET_PAGE_SIZEand the PMP region.This comment points out that we should have the smallest region, so I'm not clear why we need this change. Can you update the commit description to be clear on why this change is needed and what it fixes?
Quoted 7 lines
This function return
tlb_sizeto 1 to make the tlb uncached. However, In previous implementation,only the matched PMP entry of current access address is taken into consideration. Then, if other PMP entry
that match other address in the same page, we may also cannot cache the tlb, since different address
in that page may have different permission rights.
It doesn't matter. As the tlb size < page size, this tlb will have a TLB_INVALID_MASK flag and never match.
For this page, every access will repeat the MMU check and TLB fill.
It is not fast, but with no error.
Zhiwei
Quoted 50 lines
Regards,
Weiwei Li
Quoted 45 lines
Alistair
Quoted 43 lines
- *
- * If the size is less thenTARGET_PAGE_SIZEwe drop the size to 1.
+ * If any start address or the end address of PMP entry is presented
+ * in the TLB page and cannot override the whole TLB page we drop the
+ * size to 1.
* This means the result isn't cached in the TLB and is only used for
* a single translation.
*/
- return 1;
+ if (((pmp_sa>=tlb_sa&&pmp_sa<=tlb_ea) ||
+ (pmp_ea>=tlb_sa&&pmp_ea<=tlb_ea)) &&
+ !(pmp_sa ==tlb_sa&&pmp_ea==tlb_ea)) {
+ return 1;
+ }
}
+
+ returnTARGET_PAGE_SIZE;
}/*
target/riscv/pmp.h +1
-2@@ -76,8 +76,7 @@ int pmp_hart_has_privs(CPURISCVState *env,target_ulong addr, target_ulong size, pmp_priv_t privs, pmp_priv_t *allowed_privs, target_ulong mode); -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, - target_ulong tlb_sa, target_ulong tlb_ea); +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr); void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index);void
pmp_update_rule_nums(CPURISCVState *env);
uint32_tpmp_get_num_rules(CPURISCVState *env);Signature · 1 line
2.25.1
Re: [PATCH 1/6] target/riscv: Update pmp_get_tlb_size()
Quoted 80 lines · LIU Zhiwei
Quoted 78 lines · Weiwei Li
Quoted 62 lines · Alistair Francis
Quoted 61 lines · Weiwei Li
Not only the matched PMP entry, Any PMP entry that overlap with partial of the tlb page may make the regions in that page have different permission rights. So all of them should be taken into consideration.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
target/riscv/cpu_helper.c| 7 ++----- target/riscv/pmp.c | 34 +++++++++++++++++++++------------- target/riscv/pmp.h | 3 +--3 files changed, 24 insertions(+), 20 deletions(-)target/riscv/cpu_helper.c +2
-5@@ -703,11 +703,8 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, } *prot = pmp_priv_to_page_prot(pmp_priv); - if ((tlb_size != NULL) && pmp_index != MAX_RISCV_PMPS) { - target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1); - target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1; - - *tlb_size = pmp_get_tlb_size(env, pmp_index, tlb_sa, tlb_ea); + if (tlb_size != NULL) { + *tlb_size = pmp_get_tlb_size(env, addr); }return
TRANSLATE_SUCCESS;target/riscv/pmp.c +11
-5@@ -601,28 +601,36 @@ target_ulong mseccfg_csr_read(CPURISCVState *env) } /* -* Calculate the TLB size if the start address or the end address of +* Calculate the TLB size if any start address or the end address of * PMP entry is presented in the TLB page. */ -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, - target_ulong tlb_sa, target_ulong tlb_ea) +target_ulong pmp_get_tlb_size(CPURISCVState *env, target_ulong addr) { - target_ulong pmp_sa = env->pmp_state.addr[pmp_index].sa; - target_ulong pmp_ea = env->pmp_state.addr[pmp_index].ea; + target_ulong pmp_sa; + target_ulong pmp_ea; + target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1); + target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1; + int i; + + for (i = 0; i < MAX_RISCV_PMPS; i++) { + pmp_sa = env->pmp_state.addr[i].sa; + pmp_ea = env->pmp_state.addr[i].ea;Quoted 11 lines
Quoted 7 lines
target/riscv/pmp.c +0
-4- if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) { - return TARGET_PAGE_SIZE; - } else { /* -* At this point we have a tlb_size that is the smallest possible size
- * That fits within aTARGET_PAGE_SIZEand the PMP region.This comment points out that we should have the smallest region, so I'm not clear why we need this change. Can you update the commit description to be clear on why this change is needed and what it fixes?
This function return
tlb_sizeto 1 to make the tlb uncached. However, In previous implementation,
Quoted 7 lines
Quoted 5 lines
only the matched PMP entry of current access address is taken into consideration. Then, if other PMP entry
that match other address in the same page, we may also cannot cache the tlb, since different address
in that page may have different permission rights.
It doesn't matter. As the tlb size < page size, this tlb will have a
TLB_INVALID_MASKflag and never match.
This is what I want. However, tlb size will be page size without this patch in some cases.
Assuming:
PMP0: sa: 0x80000008 ea: 0x8000000f, rights: R
PMP1: sa: 0, ea: 0xffffffff, rights: RWX
If we try to write data to 0x80000000, PMP1 will be matched, In previous implementation,
tlb_size will be PMP1 TARGET_PAGE_SIZE and this will be cached, since only matched PMP is checked ,
and PMP1 covers the whole page. Then when we try to write data to 0x80000008, the tlb will be hit,
and this access bypass the PMP check of PMP0.
Regards,
Weiwei Li
Quoted 57 lines
For this page, every access will repeat the MMU check and TLB fill.
It is not fast, but with no error.
Zhiwei
Quoted 50 lines
Regards,
Weiwei Li
Quoted 45 lines
Alistair
Quoted 43 lines
- *
- * If the size is less thenTARGET_PAGE_SIZEwe drop the size to 1.
+ * If any start address or the end address of PMP entry is presented
+ * in the TLB page and cannot override the whole TLB page we drop the
+ * size to 1.
* This means the result isn't cached in the TLB and is only used for
* a single translation.
*/
- return 1;
+ if (((pmp_sa>=tlb_sa&&pmp_sa<=tlb_ea) ||
+ (pmp_ea>=tlb_sa&&pmp_ea<=tlb_ea)) &&
+ !(pmp_sa ==tlb_sa&&pmp_ea==tlb_ea)) {
+ return 1;
+ }
}
+
+ returnTARGET_PAGE_SIZE;
}/*
target/riscv/pmp.h +1
-2@@ -76,8 +76,7 @@ int pmp_hart_has_privs(CPURISCVState *env,target_ulong addr, target_ulong size, pmp_priv_t privs, pmp_priv_t *allowed_privs, target_ulong mode); -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, - target_ulong tlb_sa, target_ulong tlb_ea); +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr); void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index);void
pmp_update_rule_nums(CPURISCVState *env);
uint32_tpmp_get_num_rules(CPURISCVState *env);Signature · 1 line
2.25.1
LIU Zhiwei
Reviewed-byRe: [PATCH 1/6] target/riscv: Update pmp_get_tlb_size()
Quoted 99 lines · Weiwei Li
Quoted 81 lines · LIU Zhiwei
Quoted 79 lines · Weiwei Li
Quoted 63 lines · Alistair Francis
Quoted 62 lines · Weiwei Li
Not only the matched PMP entry, Any PMP entry that overlap with partial of the tlb page may make the regions in that page have different permission rights. So all of them should be taken into consideration.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
target/riscv/cpu_helper.c| 7 ++----- target/riscv/pmp.c | 34 +++++++++++++++++++++------------- target/riscv/pmp.h | 3 +--3 files changed, 24 insertions(+), 20 deletions(-)target/riscv/cpu_helper.c +2
-5@@ -703,11 +703,8 @@ static intget_physical_address_pmp (CPURISCVState *env, int *prot, } *prot = pmp_priv_to_page_prot(pmp_priv); - if ((tlb_size != NULL) && pmp_index != MAX_RISCV_PMPS) { - target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1); - target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1; - - *tlb_size = pmp_get_tlb_size(env, pmp_index, tlb_sa, tlb_ea); + if (tlb_size != NULL) { + *tlb_size = pmp_get_tlb_size(env, addr); }return
TRANSLATE_SUCCESS;target/riscv/pmp.c +11
-5@@ -601,28 +601,36 @@ target_ulong mseccfg_csr_read(CPURISCVState *env) } /* -* Calculate the TLB size if the start address or the end address of +* Calculate the TLB size if any start address or the end address of * PMP entry is presented in the TLB page. */ -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, - target_ulong tlb_sa, target_ulong tlb_ea) +target_ulong pmp_get_tlb_size(CPURISCVState *env, target_ulong addr) { - target_ulong pmp_sa = env->pmp_state.addr[pmp_index].sa; - target_ulong pmp_ea = env->pmp_state.addr[pmp_index].ea; + target_ulong pmp_sa; + target_ulong pmp_ea; + target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1); + target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1; + int i; + + for (i = 0; i < MAX_RISCV_PMPS; i++) { + pmp_sa = env->pmp_state.addr[i].sa; + pmp_ea = env->pmp_state.addr[i].ea;Quoted 11 lines
Quoted 7 lines
target/riscv/pmp.c +0
-4- if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) { - return TARGET_PAGE_SIZE; - } else { /* -* At this point we have a tlb_size that is the smallest possible size
- * That fits within aTARGET_PAGE_SIZEand the PMP region.This comment points out that we should have the smallest region, so I'm not clear why we need this change. Can you update the commit description to be clear on why this change is needed and what it fixes?
This function return
tlb_sizeto 1 to make the tlb uncached. However, In previous implementation,Quoted 7 lines
Quoted 5 lines
only the matched PMP entry of current access address is taken into consideration. Then, if other PMP entry
that match other address in the same page, we may also cannot cache the tlb, since different address
in that page may have different permission rights.
It doesn't matter. As the tlb size < page size, this tlb will have a
TLB_INVALID_MASKflag and never match.This is what I want. However, tlb size will be page size without this patch in some cases.
Assuming:
PMP0: sa: 0x80000008 ea:
0x8000000f, rights: RPMP1: sa: 0, ea:
0xffffffff, rights: RWX
Quoted 7 lines
If we try to write data to 0x80000000, PMP1 will be matched, In previous implementation,
tlb_sizewill be PMP1TARGET_PAGE_SIZEand this will be cached, since only matched PMP is checked ,and PMP1 covers the whole page. Then when we try to write data to
0x80000008, the tlb will be hit,and this access bypass the PMP check of PMP0.
I see. You are fixing the priority of PMP check rule.
You can still pass the matched index to pmp_get_tlb_size. And only check first match index PMP rules.
Quoted 41 lines
Regards,
Weiwei Li
Quoted 36 lines
For this page, every access will repeat the MMU check and TLB fill.
It is not fast, but with no error.
Zhiwei
Quoted 29 lines
Regards,
Weiwei Li
Quoted 24 lines
Alistair
Quoted 22 lines
- *
- * If the size is less thenTARGET_PAGE_SIZEwe drop the size to 1.
+ * If any start address or the end address of PMP entry is presented
+ * in the TLB page and cannot override the whole TLB page we drop the
+ * size to 1.
* This means the result isn't cached in the TLB and is only used for
* a single translation.
*/
- return 1;
+ if (((pmp_sa>=tlb_sa&&pmp_sa<=tlb_ea) ||
+ (pmp_ea>=tlb_sa&&pmp_ea<=tlb_ea)) &&
+ !(pmp_sa ==tlb_sa&&pmp_ea==tlb_ea)) {
+ return 1;
+ }
}
+
+ returnTARGET_PAGE_SIZE;
}
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
/*
target/riscv/pmp.h +0
-0@@ -76,8 +76,7 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/pmp.h int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
Quoted 8 lines
target/riscv/pmp.h +1
-2target_ulong size, pmp_priv_t privs, pmp_priv_t *allowed_privs, target_ulong mode); -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, - target_ulong tlb_sa, target_ulong tlb_ea); +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr);
In this way, you need not to change the declaration of pmp_get_tlb_size.
Otherwise,
Zhiwei
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
void
pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index); voidpmp_update_rule_nums(CPURISCVState *env);
uint32_tpmp_get_num_rules(CPURISCVState *env);Signature · 1 line
2.25.1
Re: [PATCH 1/6] target/riscv: Update pmp_get_tlb_size()
Quoted 106 lines · LIU Zhiwei
Quoted 104 lines · Weiwei Li
Quoted 84 lines · LIU Zhiwei
Quoted 82 lines · Weiwei Li
Quoted 66 lines · Alistair Francis
Quoted 65 lines · Weiwei Li
Not only the matched PMP entry, Any PMP entry that overlap with partial of the tlb page may make the regions in that page have different permission rights. So all of them should be taken into consideration.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
target/riscv/cpu_helper.c| 7 ++----- target/riscv/pmp.c | 34 +++++++++++++++++++++------------- target/riscv/pmp.h | 3 +--3 files changed, 24 insertions(+), 20 deletions(-)target/riscv/cpu_helper.c +2
-5@@ -703,11 +703,8 @@ static intget_physical_address_pmp (CPURISCVState *env, int *prot, } *prot = pmp_priv_to_page_prot(pmp_priv); - if ((tlb_size != NULL) && pmp_index != MAX_RISCV_PMPS) { - target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1); - target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1; - - *tlb_size = pmp_get_tlb_size(env, pmp_index, tlb_sa, tlb_ea); + if (tlb_size != NULL) { + *tlb_size = pmp_get_tlb_size(env, addr); }return
TRANSLATE_SUCCESS;target/riscv/pmp.c +0
-1@@ -601,28 +601,36 @@ target_ulong mseccfg_csr_read(CPURISCVState *env) } /* -* Calculate the TLB size if the start address or the end address of
+ * Calculate the TLB size if any start address or the end address of
* PMP entry is presented in the TLB page.
*/
-target_ulong pmp_get_tlb_size(CPURISCVState *env, intpmp_index, -target_ulongtlb_sa,target_ulongtlb_ea)
+target_ulongpmp_get_tlb_size(CPURISCVState *env, target_ulong addr)
{
-target_ulongpmp_sa= env->pmp_state.addr[pmp_index].sa; -target_ulongpmp_ea= env->pmp_state.addr[pmp_index].ea; +target_ulongpmp_sa;
+target_ulongpmp_ea;
+target_ulongtlb_sa= addr & ~(TARGET_PAGE_SIZE - 1); +target_ulongtlb_ea=tlb_sa+TARGET_PAGE_SIZE- 1; + int i;
+
+ for (i = 0; i <MAX_RISCV_PMPS; i++) {
+pmp_sa= env->pmp_state.addr[i].sa;
+pmp_ea= env->pmp_state.addr[i].ea;Quoted 11 lines
Quoted 7 lines
- if (
pmp_sa<=tlb_sa&&pmp_ea>=tlb_ea) {
- returnTARGET_PAGE_SIZE;
- } else {
/*
- * At this point we have atlb_sizethat is the smallest possible size
- * That fits within aTARGET_PAGE_SIZEand the PMP region.This comment points out that we should have the smallest region, so I'm not clear why we need this change. Can you update the commit description to be clear on why this change is needed and what it fixes?
This function return
tlb_sizeto 1 to make the tlb uncached. However, In previous implementation,Quoted 7 lines
Quoted 5 lines
only the matched PMP entry of current access address is taken into consideration. Then, if other PMP entry
that match other address in the same page, we may also cannot cache the tlb, since different address
in that page may have different permission rights.
It doesn't matter. As the tlb size < page size, this tlb will have a
TLB_INVALID_MASKflag and never match.This is what I want. However, tlb size will be page size without this patch in some cases.
Assuming:
PMP0: sa: 0x80000008 ea:
0x8000000f, rights: RPMP1: sa: 0, ea:
0xffffffff, rights: RWXIf we try to write data to 0x80000000, PMP1 will be matched, In previous implementation,
Quoted 7 lines
Quoted 5 lines
tlb_sizewill be PMP1TARGET_PAGE_SIZEand this will be cached, since only matched PMP is checked ,and PMP1 covers the whole page. Then when we try to write data to
0x80000008, the tlb will be hit,and this access bypass the PMP check of PMP0.
I see. You are fixing the priority of PMP check rule.
Yeah. You can see it as a priority problem.
Quoted 2 lines
You can still pass the matched index to pmp_get_tlb_size. And only check first match index PMP rules.
Yeah. only the PMP rules before the matched PMP need be checked.
However, I prefers separate the matched index from pmp_get_tlb_size, then we can separate
this function from get_physical_address_pmp (not all pmp check needs caculate the tlb size).
Maybe we can improve the check to following code:
if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) {
return TARGET_PAGE_SIZE;
} else if ((pmp_sa >= tlb_sa && pmp_sa <= tlb_ea) || (pmp_ea >= tlb_sa && pmp_ea <= tlb_ea)) { return 1;
}
then the checked index will not be larger than matched index(the matched case will match one of the above conditions).
Regards,
Weiwei Li
Quoted 72 lines
Quoted 42 lines
Regards,
Weiwei Li
Quoted 36 lines
For this page, every access will repeat the MMU check and TLB fill.
It is not fast, but with no error.
Zhiwei
Quoted 29 lines
Regards,
Weiwei Li
Quoted 24 lines
Alistair
Quoted 22 lines
- *
- * If the size is less thenTARGET_PAGE_SIZEwe drop the size to 1.
+ * If any start address or the end address of PMP entry is presented
+ * in the TLB page and cannot override the whole TLB
page we drop the
+ * size to 1.
* This means the result isn't cached in the TLB and is only used for
* a single translation.
*/
- return 1;
+ if (((pmp_sa>=tlb_sa&&pmp_sa<=tlb_ea) ||
+ (pmp_ea>=tlb_sa&&pmp_ea<=tlb_ea)) &&
+ !(pmp_sa ==tlb_sa&&pmp_ea==tlb_ea)) {
+ return 1;
+ }
}
+
+ returnTARGET_PAGE_SIZE;
}Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
/*
target/riscv/pmp.h +0
-0@@ -76,8 +76,7 @@ int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
target/riscv/pmp.h int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
Quoted 8 lines
target/riscv/pmp.h +1
-2target_ulong size, pmp_priv_t privs, pmp_priv_t *allowed_privs, target_ulong mode); -target_ulong pmp_get_tlb_size (CPURISCVState *env,int pmp_index, - target_ulong tlb_sa, target_ulong tlb_ea); +target_ulong pmp_get_tlb_size (CPURISCVState *env,target_ulong addr);In this way, you need not to change the declaration of
pmp_get_tlb_size.Otherwise,
Reviewed-by LIU Zhiwei <[email protected]>Zhiwei
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
void
pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index); voidpmp_update_rule_nums(CPURISCVState *env);
uint32_tpmp_get_num_rules(CPURISCVState *env);Signature · 1 line
2.25.1
PATCH 2/6 target/riscv: Move pmp_get_tlb_size apart from get_physical_address_pmp
In reply to Weiwei Lipmp_get_tlb_size have no relationship with pmp-related permission check currently.
--- target/riscv/cpu_helper.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
target/riscv/cpu_helper.c +6 -10
@@ -676,14 +676,11 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv)* * @env: CPURISCVState * @prot: The returned protection attributes -* @tlb_size: TLB page size containing addr. It could be modified after PMP -* permission checking. NULL if not set TLB page for addr. * @addr: The physical address to be checked permission * @access_type: The type of MMU access * @mode: Indicates current privilege level. */ -static int get_physical_address_pmp (CPURISCVState *env,int *prot, -target_ulong *tlb_size,hwaddr addr, +static int get_physical_address_pmp (CPURISCVState *env,int *prot,hwaddr addr,int size,MMUAccessType access_type,int mode) {
@@ -703,9 +700,6 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, } *prot =pmp_priv_to_page_prot (pmp_priv); -if (tlb_size !=NULL ) { - *tlb_size =pmp_get_tlb_size (env, addr); - }return TRANSLATE_SUCCESS ; }
@@ -905,7 +899,7 @@ restart: }int pmp_prot; -int pmp_ret =get_physical_address_pmp (env, &pmp_prot,NULL , pte_addr, +int pmp_ret =get_physical_address_pmp (env, &pmp_prot, pte_addr,sizeof (target_ulong),MMU_DATA_LOAD ,PRV_S );if (pmp_ret !=TRANSLATE_SUCCESS ) {
@@ -1300,8 +1294,9 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, prot &= prot2;if (ret ==TRANSLATE_SUCCESS ) { - ret =get_physical_address_pmp (env, &prot_pmp, &tlb_size, pa, + ret =get_physical_address_pmp (env, &prot_pmp, pa, size, access_type, mode); + tlb_size =pmp_get_tlb_size (env, pa);qemu_log_mask (CPU_LOG_MMU , "%s PMP address=" HWADDR_FMT_plx " ret %d prot"
@@ -1333,8 +1328,9 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, __func__, address, ret, pa, prot);if (ret ==TRANSLATE_SUCCESS ) { - ret =get_physical_address_pmp (env, &prot_pmp, &tlb_size, pa, + ret =get_physical_address_pmp (env, &prot_pmp, pa, size, access_type, mode); + tlb_size =pmp_get_tlb_size (env, pa);qemu_log_mask (CPU_LOG_MMU , "%s PMP address=" HWADDR_FMT_plx " ret %d prot"
Signature · 1 line
2.25.1
Re: [PATCH 2/6] target/riscv: Move pmp_get_tlb_size apart from get_physical_address_pmp
Quoted 6 lines · Weiwei Li
pmp_get_tlb_sizehave no relationship with pmp-related permission check currently.Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>
Alistair
Quoted 69 lines
--- target/riscv/cpu_helper.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)target/riscv/cpu_helper.c +6
-11@@ -676,14 +676,11 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv)* * @env: CPURISCVState * @prot: The returned protection attributes -* @tlb_size: TLB page size containing addr. It could be modified after PMP -* permission checking. NULL if not set TLB page for addr. * @addr: The physical address to be checked permission * @access_type: The type of MMU access * @mode: Indicates current privilege level. */ -static int get_physical_address_pmp (CPURISCVState *env,int *prot, -target_ulong *tlb_size,hwaddr addr, +static int get_physical_address_pmp (CPURISCVState *env,int *prot,hwaddr addr,int size,MMUAccessType access_type,int mode) {@@ -703,9 +700,6 @@ static int get_physical_address_pmp(CPURISCVState *env, int *prot, } *prot =pmp_priv_to_page_prot (pmp_priv); -if (tlb_size !=NULL ) { - *tlb_size =pmp_get_tlb_size (env, addr); - }return TRANSLATE_SUCCESS ; }@@ -905,7 +899,7 @@ restart: }int pmp_prot; -int pmp_ret =get_physical_address_pmp (env, &pmp_prot,NULL , pte_addr, +int pmp_ret =get_physical_address_pmp (env, &pmp_prot, pte_addr,sizeof (target_ulong),MMU_DATA_LOAD ,PRV_S );if (pmp_ret !=TRANSLATE_SUCCESS ) {@@ -1300,8 +1294,9 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, prot &= prot2;if (ret ==TRANSLATE_SUCCESS ) { - ret =get_physical_address_pmp (env, &prot_pmp, &tlb_size, pa, + ret =get_physical_address_pmp (env, &prot_pmp, pa, size, access_type, mode); + tlb_size =pmp_get_tlb_size (env, pa);qemu_log_mask (CPU_LOG_MMU , "%s PMP address=" HWADDR_FMT_plx " ret %d prot"@@ -1333,8 +1328,9 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, __func__, address, ret, pa, prot);if (ret ==TRANSLATE_SUCCESS ) { - ret =get_physical_address_pmp (env, &prot_pmp, &tlb_size, pa, + ret =get_physical_address_pmp (env, &prot_pmp, pa, size, access_type, mode); + tlb_size =pmp_get_tlb_size (env, pa);qemu_log_mask (CPU_LOG_MMU , "%s PMP address=" HWADDR_FMT_plx " ret %d prot" --2.25.1
PATCH 3/6 target/riscv: flush tlb when pmpaddr is updated
In reply to Weiwei LiTLB should be flushed not only for pmpcfg csr changes, but also for pmpaddr csr changes.
--- target/riscv/pmp.c | 1 + 1 file changed, 1 insertion(+)
target/riscv/pmp.c +1 -0
@@ -537,6 +537,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,if (!pmp_is_locked (env, addr_index)) { env->pmp_state.pmp[addr_index].addr_reg = val;pmp_update_rule (env, addr_index); +tlb_flush (env_cpu (env)); }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpaddr write - locked\n" );
Signature · 1 line
2.25.1
Re: [PATCH 3/6] target/riscv: flush tlb when pmpaddr is updated
Quoted 6 lines · Weiwei Li
TLB should be flushed not only for pmpcfg csr changes, but also for pmpaddr csr changes.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>
Alistair
Quoted 20 lines
--- target/riscv/pmp.c | 1 + 1 file changed, 1 insertion(+)target/riscv/pmp.c +1
-1@@ -537,6 +537,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,if (!pmp_is_locked (env, addr_index)) { env->pmp_state.pmp[addr_index].addr_reg = val;pmp_update_rule (env, addr_index); +tlb_flush (env_cpu (env)); }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpaddr write - locked\n" ); --2.25.1
Re: [PATCH 3/6] target/riscv: flush tlb when pmpaddr is updated
In reply to Weiwei LiQuoted 14 lines · Weiwei Li
TLB should be flushed not only for pmpcfg csr changes, but also for pmpaddr csr changes.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>--- target/riscv/pmp.c | 1 + 1 file changed, 1 insertion(+)target/riscv/pmp.c +0
-0
target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, · line 540
Quoted 4 lines
target/riscv/pmp.c +1
-0@@ -537,6 +537,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,if (!pmp_is_locked (env, addr_index)) { env->pmp_state.pmp[addr_index].addr_reg = val;pmp_update_rule (env, addr_index); +tlb_flush (env_cpu (env));
Can we always flush tlb in pmp_update_rule?
Zhiwei
Quoted 3 lines
target/riscv/pmp.c +0
-0}else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpaddr write - locked\n" );
Re: [PATCH 3/6] target/riscv: flush tlb when pmpaddr is updated
Quoted 11 lines · LIU Zhiwei
Quoted 9 lines · Weiwei Li
TLB should be flushed not only for pmpcfg csr changes, but also for pmpaddr csr changes.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
target/riscv/pmp.c| 1 +1 file changed, 1 insertion(+)
Quoted 12 lines
target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, · line 541
Quoted 10 lines
target/riscv/pmp.c +1
-0@@ -537,6 +537,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, if (!pmp_is_locked(env, addr_index)) { env->pmp_state.pmp[addr_index].addr_reg = val; pmp_update_rule(env, addr_index); + tlb_flush(env_cpu(env));Can we always flush tlb in
pmp_update_rule?
I considered this way, However this may lead to tlb being flushed several times when we update pmpcfg csrs.
Regards,
Weiwei Li
Quoted 6 lines
Zhiwei
Quoted 3 lines
target/riscv/pmp.c +0
-0} else { qemu_log_mask(LOG_GUEST_ERROR,"ignoring pmpaddr write - locked\n");
PATCH 4/6 target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes
In reply to Weiwei LiTLB needn't be flushed when pmpcfg/pmpaddr don't changes.
--- target/riscv/pmp.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
target/riscv/pmp.c +16 -8
@@ -26,7 +26,7 @@#include "trace.h" #include "exec/exec-all.h" -static void pmp_write_cfg (CPURISCVState *env,uint32_t addr_index, +static bool pmp_write_cfg (CPURISCVState *env,uint32_t addr_index,uint8_t val);static uint8_t pmp_read_cfg (CPURISCVState *env,uint32_t addr_index);static void pmp_update_rule (CPURISCVState *env,uint32_t pmp_index);
@@ -83,7 +83,7 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)* Accessor to set the cfg reg for a specific PMP/HART * Bounds checks and relevant lock bit. */ -static void pmp_write_cfg (CPURISCVState *env,uint32_t pmp_index,uint8_t val) +static bool pmp_write_cfg (CPURISCVState *env,uint32_t pmp_index,uint8_t val) {if (pmp_index <MAX_RISCV_PMPS ) {bool locked = true;
@@ -119,14 +119,17 @@ static void pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)if (locked) {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpcfg write - locked\n" ); - }else { + }else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) { env->pmp_state.pmp[pmp_index].cfg_reg = val;pmp_update_rule (env, pmp_index); +return true; } }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpcfg write - out of bounds\n" ); } + +return false; } static void pmp_decode_napot(target_ulong a, target_ulong *sa,
@@ -477,16 +480,19 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,int i;uint8_t cfg_val;int pmpcfg_nums =2 <<riscv_cpu_mxl (env); +bool modified = false;trace_pmpcfg_csr_write (env->mhartid, reg_index, val);for (i =0 ; i < pmpcfg_nums; i++) { cfg_val = (val >>8 * i) &0xff ; -pmp_write_cfg (env, (reg_index *4 ) + i, cfg_val); + modified |=pmp_write_cfg (env, (reg_index *4 ) + i, cfg_val); }/* If PMP permission of any addr has been changed, flush TLB pages. */ -tlb_flush (env_cpu (env)); +if (modified) { +tlb_flush (env_cpu (env)); + } }
@@ -535,9 +541,11 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, }if (!pmp_is_locked (env, addr_index)) { - env->pmp_state.pmp[addr_index].addr_reg = val; -pmp_update_rule (env, addr_index); -tlb_flush (env_cpu (env)); +if (env->pmp_state.pmp[addr_index].addr_reg != val) { + env->pmp_state.pmp[addr_index].addr_reg = val; +pmp_update_rule (env, addr_index); +tlb_flush (env_cpu (env)); + } }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpaddr write - locked\n" );
Signature · 1 line
2.25.1
Re: [PATCH 4/6] target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes
Quoted 5 lines · Weiwei Li
TLB needn't be flushed when pmpcfg/pmpaddr don't changes.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>
Alistair
Quoted 86 lines
--- target/riscv/pmp.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)target/riscv/pmp.c +16
-9@@ -26,7 +26,7 @@#include "trace.h" #include "exec/exec-all.h" -static void pmp_write_cfg (CPURISCVState *env,uint32_t addr_index, +static bool pmp_write_cfg (CPURISCVState *env,uint32_t addr_index,uint8_t val);static uint8_t pmp_read_cfg (CPURISCVState *env,uint32_t addr_index);static void pmp_update_rule (CPURISCVState *env,uint32_t pmp_index);@@ -83,7 +83,7 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)* Accessor to set the cfg reg for a specific PMP/HART * Bounds checks and relevant lock bit. */ -static void pmp_write_cfg (CPURISCVState *env,uint32_t pmp_index,uint8_t val) +static bool pmp_write_cfg (CPURISCVState *env,uint32_t pmp_index,uint8_t val) {if (pmp_index <MAX_RISCV_PMPS ) {bool locked = true;@@ -119,14 +119,17 @@ static void pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)if (locked) {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpcfg write - locked\n" ); - }else { + }else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) { env->pmp_state.pmp[pmp_index].cfg_reg = val;pmp_update_rule (env, pmp_index); +return true; } }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpcfg write - out of bounds\n" ); } + +return false; } static void pmp_decode_napot(target_ulong a, target_ulong *sa,@@ -477,16 +480,19 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,int i;uint8_t cfg_val;int pmpcfg_nums =2 <<riscv_cpu_mxl (env); +bool modified = false;trace_pmpcfg_csr_write (env->mhartid, reg_index, val);for (i =0 ; i < pmpcfg_nums; i++) { cfg_val = (val >>8 * i) &0xff ; -pmp_write_cfg (env, (reg_index *4 ) + i, cfg_val); + modified |=pmp_write_cfg (env, (reg_index *4 ) + i, cfg_val); }/* If PMP permission of any addr has been changed, flush TLB pages. */ -tlb_flush (env_cpu (env)); +if (modified) { +tlb_flush (env_cpu (env)); + } }@@ -535,9 +541,11 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, }if (!pmp_is_locked (env, addr_index)) { - env->pmp_state.pmp[addr_index].addr_reg = val; -pmp_update_rule (env, addr_index); -tlb_flush (env_cpu (env)); +if (env->pmp_state.pmp[addr_index].addr_reg != val) { + env->pmp_state.pmp[addr_index].addr_reg = val; +pmp_update_rule (env, addr_index); +tlb_flush (env_cpu (env)); + } }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpaddr write - locked\n" ); --2.25.1
Re: [PATCH 4/6] target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes
In reply to Weiwei LiQuoted 1 line · Weiwei Li
TLB needn't be flushed when pmpcfg/pmpaddr don't changes.
If we flush the tlb in pmp_update_rules, we don't need this patch.
Zhiwei
Quoted 85 lines
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>--- target/riscv/pmp.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)target/riscv/pmp.c +16
-8@@ -26,7 +26,7 @@#include "trace.h" #include "exec/exec-all.h" -static void pmp_write_cfg (CPURISCVState *env,uint32_t addr_index, +static bool pmp_write_cfg (CPURISCVState *env,uint32_t addr_index,uint8_t val);static uint8_t pmp_read_cfg (CPURISCVState *env,uint32_t addr_index);static void pmp_update_rule (CPURISCVState *env,uint32_t pmp_index);@@ -83,7 +83,7 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)* Accessor to set the cfg reg for a specific PMP/HART * Bounds checks and relevant lock bit. */ -static void pmp_write_cfg (CPURISCVState *env,uint32_t pmp_index,uint8_t val) +static bool pmp_write_cfg (CPURISCVState *env,uint32_t pmp_index,uint8_t val) {if (pmp_index <MAX_RISCV_PMPS ) {bool locked = true;@@ -119,14 +119,17 @@ static void pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)if (locked) {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpcfg write - locked\n" ); - }else { + }else if (env->pmp_state.pmp[pmp_index].cfg_reg != val) { env->pmp_state.pmp[pmp_index].cfg_reg = val;pmp_update_rule (env, pmp_index); +return true; } }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpcfg write - out of bounds\n" ); } + +return false; } static void pmp_decode_napot(target_ulong a, target_ulong *sa,@@ -477,16 +480,19 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,int i;uint8_t cfg_val;int pmpcfg_nums =2 <<riscv_cpu_mxl (env); +bool modified = false;trace_pmpcfg_csr_write (env->mhartid, reg_index, val);for (i =0 ; i < pmpcfg_nums; i++) { cfg_val = (val >>8 * i) &0xff ; -pmp_write_cfg (env, (reg_index *4 ) + i, cfg_val); + modified |=pmp_write_cfg (env, (reg_index *4 ) + i, cfg_val); }/* If PMP permission of any addr has been changed, flush TLB pages. */ -tlb_flush (env_cpu (env)); +if (modified) { +tlb_flush (env_cpu (env)); + } }@@ -535,9 +541,11 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, }if (!pmp_is_locked (env, addr_index)) { - env->pmp_state.pmp[addr_index].addr_reg = val; -pmp_update_rule (env, addr_index); -tlb_flush (env_cpu (env)); +if (env->pmp_state.pmp[addr_index].addr_reg != val) { + env->pmp_state.pmp[addr_index].addr_reg = val; +pmp_update_rule (env, addr_index); +tlb_flush (env_cpu (env)); + } }else {qemu_log_mask (LOG_GUEST_ERROR ,"ignoring pmpaddr write - locked\n" );
PATCH 5/6 target/riscv: flush tb when PMP entry changes
In reply to Weiwei LiThe translation block may also be affected when PMP entry changes.
--- target/riscv/pmp.c | 3 +++ 1 file changed, 3 insertions(+)
target/riscv/pmp.c +3 -0
@@ -25,6 +25,7 @@#include "cpu.h" #include "trace.h" #include "exec/exec-all.h" +#include "exec/tb-flush.h" static bool pmp_write_cfg (CPURISCVState *env,uint32_t addr_index,uint8_t val);
@@ -492,6 +493,7 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,/* If PMP permission of any addr has been changed, flush TLB pages. */ if (modified) {tlb_flush (env_cpu (env)); +tb_flush (env_cpu (env)); } }
@@ -545,6 +547,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, env->pmp_state.pmp[addr_index].addr_reg = val;pmp_update_rule (env, addr_index);tlb_flush (env_cpu (env)); +tb_flush (env_cpu (env)); } }else { qemu_log_mask(LOG_GUEST_ERROR,
Signature · 1 line
2.25.1
Re: [PATCH 5/6] target/riscv: flush tb when PMP entry changes
Quoted 21 lines · Weiwei Li
The translation block may also be affected when PMP entry changes.
Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>--- target/riscv/pmp.c | 3 +++ 1 file changed, 3 insertions(+)target/riscv/pmp.c +1
-0@@ -25,6 +25,7 @@#include "cpu.h" #include "trace.h" #include "exec/exec-all.h" +#include "exec/tb-flush.h" static bool pmp_write_cfg (CPURISCVState *env,uint32_t addr_index,uint8_t val);
target/riscv/pmp.c void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, · line 496
Quoted 4 lines
target/riscv/pmp.c +1
-0@@ -492,6 +493,7 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,/* If PMP permission of any addr has been changed, flush TLB pages. */ if (modified) {tlb_flush (env_cpu (env)); +tb_flush (env_cpu (env));
Move this to pmp_update_rules.
Zhiwei
Quoted 11 lines
target/riscv/pmp.c +1
-0} }@@ -545,6 +547,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, env->pmp_state.pmp[addr_index].addr_reg = val;pmp_update_rule (env, addr_index);tlb_flush (env_cpu (env)); +tb_flush (env_cpu (env)); } }else { qemu_log_mask(LOG_GUEST_ERROR,
PATCH 6/6 accel/tcg: Remain TLB_INVALID_MASK in the address when TLB is re-filled
In reply to Weiwei LiWhen PMP entry overlap part of the page, we'll set the tlb_size to 1, and this will make the address set with TLB_INVALID_MASK to make the page un-cached. However, if we clear TLB_INVALID_MASK when TLB is re-filled, then the TLB host address will be cached, and the following instructions can use this host address directly which may lead to the bypass of PMP related check.
--- accel/tcg/cputlb.c | 7 ------- 1 file changed, 7 deletions(-)
accel/tcg/cputlb.c +0 -7
@@ -1563,13 +1563,6 @@ static int probe_access_internal(CPUArchState *env, target_ulong addr,/* TLB resize via tlb_fill may have moved the entry. */ index =tlb_index (env, mmu_idx, addr); entry =tlb_entry (env, mmu_idx, addr); - -/* -* With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately, -* to force the next access through tlb_fill. We've just -* called tlb_fill, so we know that this entry *is* valid. -*/ - flags &= ~TLB_INVALID_MASK ; } tlb_addr =tlb_read_ofs (entry, elt_ofs); }
Signature · 1 line
2.25.1
Re: [PATCH 6/6] accel/tcg: Remain TLB_INVALID_MASK in the address when TLB is re-filled
Quoted 9 lines · Weiwei Li
When PMP entry overlap part of the page, we'll set the
tlb_sizeto 1, and this will make the address set withTLB_INVALID_MASKto make the page un-cached. However, if we clearTLB_INVALID_MASKwhen TLB is re-filled, then the TLB host address will be cached, and the following instructions can use this host address directly which may lead to the bypass of PMP related check.Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
For this commit I believe it's worth mentioning that it's partially reverting commit c3c8bf579b431b6b ("accel/tcg: Suppress auto-invalidate in probe_access_internal") that was made to handle a particularity/quirk that was present in s390x code.
At first glance this patch seems benign but we must make sure that no other assumptions were made with this particular change in probe_access_internal().
Thanks,
Daniel
Quoted 21 lines
accel/tcg/cputlb.c | 7 ------- 1 file changed, 7 deletions(-)accel/tcg/cputlb.c +0
-7@@ -1563,13 +1563,6 @@ static int probe_access_internal(CPUArchState *env, target_ulong addr,/* TLB resize via tlb_fill may have moved the entry. */ index =tlb_index (env, mmu_idx, addr); entry =tlb_entry (env, mmu_idx, addr); - -/* -* With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately, -* to force the next access through tlb_fill. We've just -* called tlb_fill, so we know that this entry *is* valid. -*/ - flags &= ~TLB_INVALID_MASK ; } tlb_addr =tlb_read_ofs (entry, elt_ofs); }
Re: [PATCH 6/6] accel/tcg: Remain TLB_INVALID_MASK in the address when TLB is re-filled
Quoted 13 lines · Daniel Henrique Barboza
Quoted 9 lines · Weiwei Li
When PMP entry overlap part of the page, we'll set the
tlb_sizeto 1, and this will make the address set withTLB_INVALID_MASKto make the page un-cached. However, if we clearTLB_INVALID_MASKwhen TLB is re-filled, then the TLB host address will be cached, and the following instructions can use this host address directly which may lead to the bypass of PMP related check.Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
Quoted 7 lines
For this commit I believe it's worth mentioning that it's partially reverting commit
c3c8bf579b431b6b("accel/tcg: Suppress auto-invalidate inprobe_access_internal") that was made to handle a particularity/quirk that was present in s390x code.At first glance this patch seems benign but we must make sure that no other assumptions were made with this particular change in
probe_access_internal().
I think this change will introduce no external function change except that we should
always walk the page table(fill_tlb) for memory access to that page. And this is needed
for pages that are partially overlapped by PMP region.
Regards,
Weiwei Li
Quoted 29 lines
Thanks,
Daniel
Quoted 23 lines
accel/tcg/cputlb.c| 7 -------1 file changed, 7 deletions(-)accel/tcg/cputlb.c +0
-7@@ -1563,13 +1563,6 @@ static int probe_access_internal(CPUArchState *env, target_ulong addr, /* TLB resize via tlb_fill may have moved the entry. */ index = tlb_index(env, mmu_idx, addr); entry = tlb_entry(env, mmu_idx, addr); - - /* - * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately, - * to force the next access through tlb_fill. We've just - * called tlb_fill, so we know that this entry *is* valid. - */ - flags &= ~TLB_INVALID_MASK; }
tlb_addr=tlb_read_ofs(entry, elt_ofs); }
Re: [PATCH 6/6] accel/tcg: Remain TLB_INVALID_MASK in the address when TLB is re-filled
In reply to Daniel Henrique BarbozaQuoted 29 lines · Daniel Henrique Barboza
Quoted 9 lines · Weiwei Li
When PMP entry overlap part of the page, we'll set the
tlb_sizeto 1, and this will make the address set withTLB_INVALID_MASKto make the page un-cached. However, if we clearTLB_INVALID_MASKwhen TLB is re-filled, then the TLB host address will be cached, and the following instructions can use this host address directly which may lead to the bypass of PMP related check.Signed-off-by Weiwei Li <[email protected]>Signed-off-by Junqiang Wang <[email protected]>---
For this commit I believe it's worth mentioning that it's partially reverting commit
c3c8bf579b431b6b("accel/tcg: Suppress auto-invalidate inprobe_access_internal") that was made to handle a particularity/quirk that was present in s390x code.At first glance this patch seems benign but we must make sure that no other assumptions were made with this particular change in
probe_access_internal().Thanks,
Daniel
Quoted 3 lines
accel/tcg/cputlb.c| 7 -------1 file changed, 7 deletions(-)
Quoted 16 lines
Quoted 6 lines
accel/tcg/cputlb.c +0
-0@@ -1563,13 +1563,6 @@ static int probe_access_internal(CPUArchState *env, target_ulong addr,accel/tcg/cputlb.c static int probe_access_internal(CPUArchState *env, target_ulong · line 1573
Quoted 10 lines
accel/tcg/cputlb.c +0
-7/* TLB resize via tlb_fill may have moved the entry. */ index = tlb_index(env, mmu_idx, addr); entry = tlb_entry(env, mmu_idx, addr); - - /* - * With PAGE_WRITE_INV, we set TLB_INVALID_MASK immediately, - * to force the next access through tlb_fill. We've just - * called tlb_fill, so we know that this entry *is* valid. - */ - flags &= ~TLB_INVALID_MASK;
I missed the original patch, but this is definitely wrong.
Clearing this bit locally (!) is correct because we want to inform the caller of probe_access_* that the access is valid. We know that it is valid because we have just queried tlb_fill (and thus for riscv, PMP).
Clearing the bit locally does *not* cause the tlb entry to be cached -- the INVALID bit is still set within the tlb entry. The next access will again go through tlb_fill.
What is the original problem you are seeing? The commit message does not say.
r~
Re: [PATCH 6/6] accel/tcg: Remain TLB_INVALID_MASK in the address when TLB is re-filled
Quoted 8 lines · Richard Henderson
Quoted 6 lines
Quoted 6 lines
- /*
- * WithPAGE_WRITE_INV, we setTLB_INVALID_MASKimmediately, - * to force the next access through tlb_fill. We've just - * calledtlb_fill, so we know that this entry *is* valid. - */
- flags &= ~TLB_INVALID_MASK;
Quoted 7 lines
I missed the original patch, but this is definitely wrong.
Clearing this bit locally (!) is correct because we want to inform the caller of probe_access_* that the access is valid. We know that it is valid because we have just queried
tlb_fill(and thus for riscv, PMP).Clearing the bit locally does *not* cause the tlb entry to be cached -- the INVALID bit is still set within the tlb entry. The next access will again go through
tlb_fill.What is the original problem you are seeing? The commit message does not say.
From https://lore.kernel.org/qemu-devel/[email protected]/
I see that it is a problem with execution.
By eye, it appears that get_page_addr_code_hostp needs adjustment, e.g.
(void)probe_access_internal(env, addr, 1, MMU_INST_FETCH,
cpu_mmu_index(env, true), false, &p, &full, 0);
if (p == NULL) {
return -1;
}
+ if (full->lg_page_size < TARGET_PAGE_BITS) {
+ return -1;
+ }
if (hostp) {
*hostp = p;
}It seems like we could do slightly better than this, perhaps by single-stepping through such a page, but surely this edge case is so uncommon as to not make it worthwhile to consider.
r~
Re: [PATCH 6/6] accel/tcg: Remain TLB_INVALID_MASK in the address when TLB is re-filled
Quoted 15 lines · Richard Henderson
Quoted 14 lines · Richard Henderson
Quoted 8 lines
Quoted 8 lines
- /*
- * WithPAGE_WRITE_INV, we setTLB_INVALID_MASKimmediately,
- * to force the next access throughtlb_fill. We've just - * calledtlb_fill, so we know that this entry *is* valid.
- */
- flags &= ~TLB_INVALID_MASK;I missed the original patch, but this is definitely wrong.
Clearing this bit locally (!) is correct because we want to inform the caller of probe_access_* that the access is valid. We know that it is valid because we have just queried
tlb_fill(and thus for riscv, PMP).
Quoted 8 lines
Quoted 5 lines
Clearing the bit locally does *not* cause the tlb entry to be cached -- the INVALID bit is still set within the tlb entry. The next access will again go through
tlb_fill.What is the original problem you are seeing? The commit message does not say.
From https://lore.kernel.org/qemu-devel/[email protected]/
I see that it is a problem with execution.
Yeah. I found this problem in PMP check for instruction fetch.
Quoted 15 lines
By eye, it appears that
get_page_addr_code_hostpneeds adjustment, e.g.(void)probe_access_internal(env, addr, 1,
MMU_INST_FETCH,cpu_mmu_index(env, true), false, &p, &full, 0); if (p == NULL) {
return -1;
}
+ if (full->lg_page_size<TARGET_PAGE_BITS) {
+ return -1;
+ }
if (hostp) {
*hostp = p;
}
Quoted 1 line
It seems like we could do slightly better than this, perhaps by single-stepping through such a page, but surely this edge case is so uncommon as to not make it worthwhile to consider.
OK. I'll update and test it later.
Regards,
Weiwei Li
Quoted 3 lines
r~
Quoted 1 line · Weiwei Li
This patchset tries to fix the PMP bypass problem issue https://gitlab.com/qemu-project/qemu/-/issues/1542
Please add your analysis of this issue here.
By the way, I think this problem is introduced by
https://www.mail-archive.com/[email protected]/msg939331.html
I have commented on how to correct this patch. But by accident, it has been merged.
Zhiwei
Quoted 20 lines
The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-pmp-fixWeiwei Li (6):
target/riscv: Updatepmp_get_tlb_size()target/riscv: Movepmp_get_tlb_sizeapart from
get_physical_address_pmp
target/riscv: flush tlb when pmpaddr is updated target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes target/riscv: flush tb when PMP entry changes accel/tcg: RemainTLB_INVALID_MASKin the address when TLB is
re-filledaccel/tcg/cputlb.c | 7 ----- target/riscv/cpu_helper.c | 19 ++++--------- target/riscv/pmp.c | 60 ++++++++++++++++++++++++++------------- target/riscv/pmp.h | 3 +- 4 files changed, 47 insertions(+), 42 deletions(-)
Quoted 9 lines · LIU Zhiwei
Quoted 1 line · Weiwei Li
This patchset tries to fix the PMP bypass problem issue https://gitlab.com/qemu-project/qemu/-/issues/1542
Please add your analysis of this issue here.
By the way, I think this problem is introduced by
https://www.mail-archive.com/[email protected]/msg939331.html
It seems have no relationship with this commit.
I think there are several problems for this issue:
1. TLB will not be cached only when the access address have matched PMP entry. So the other address access may hit the TLB(if first access of the page didn't hit the PMP entry)
and bypass the pmp check. This is fixed by patch 1.
2. Writing to pmpaddr didn't trigger tlb flush. This is fixed by patch 3.
3. The tb isn't flushed when PMP permission changes, so It also may hit the tb and bypass the changed PMP check for instruction fetch. This is fixed by patch 5.
4. We set the tlb_size to 1 to make the TLB_INVALID_MASK set. However this flag will be cleared after fill_tlb, and this will make the host address be cached, and let the following instruction fetch in the same tb bypass the PMP check. This is fixed by patch 6.
Regards,
Weiwei Li
Quoted 25 lines
I have commented on how to correct this patch. But by accident, it has been merged.
Zhiwei
Quoted 20 lines
The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-pmp-fixWeiwei Li (6):
target/riscv: Updatepmp_get_tlb_size()target/riscv: Movepmp_get_tlb_sizeapart fromget_physical_address_pmp
target/riscv: flush tlb when pmpaddr is updated target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes target/riscv: flush tb when PMP entry changes accel/tcg: RemainTLB_INVALID_MASKin the address when TLB is re-filledaccel/tcg/cputlb.c | 7 -----
target/riscv/cpu_helper.c| 19 ++++--------- target/riscv/pmp.c | 60 ++++++++++++++++++++++++++------------- target/riscv/pmp.h | 3 +-4 files changed, 47 insertions(+), 42 deletions(-)
Quoted 10 lines · Weiwei Li
Quoted 8 lines · LIU Zhiwei
Quoted 1 line · Weiwei Li
This patchset tries to fix the PMP bypass problem issue https://gitlab.com/qemu-project/qemu/-/issues/1542
Please add your analysis of this issue here.
By the way, I think this problem is introduced by
Quoted 7 lines
It seems have no relationship with this commit.
I think there are several problems for this issue:
1. TLB will not be cached only when the access address have matched PMP entry.
TLB will be filled only when PMP check and PTW check pass.
Quoted 1 line
So the other address access may hit the TLB(if first access of the page didn't hit the PMP entry)
This page will not be filled to TLB if the first access of the page didn't pass the PMP check.
Quoted 2 lines
and bypass the pmp check. This is fixed by patch 1.
Never it should be.
Zhiwei
Quoted 37 lines
2. Writing to pmpaddr didn't trigger tlb flush. This is fixed by patch 3.
3. The tb isn't flushed when PMP permission changes, so It also may hit the tb and bypass the changed PMP check for instruction fetch. This is fixed by patch 5.
4. We set the
tlb_sizeto 1 to make theTLB_INVALID_MASKset. However this flag will be cleared afterfill_tlb, and this will make the host address be cached, and let the following instruction fetch in the same tb bypass the PMP check. This is fixed by patch 6.Regards,
Weiwei Li
Quoted 26 lines
I have commented on how to correct this patch. But by accident, it has been merged.
Zhiwei
Quoted 21 lines
The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-pmp-fixWeiwei Li (6):
target/riscv: Updatepmp_get_tlb_size()target/riscv: Movepmp_get_tlb_sizeapart fromget_physical_address_pmp
target/riscv: flush tlb when pmpaddr is updated target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes target/riscv: flush tb when PMP entry changes accel/tcg: RemainTLB_INVALID_MASKin the address when TLB is re-filledaccel/tcg/cputlb.c | 7 -----
target/riscv/cpu_helper.c| 19 ++++---------
target/riscv/pmp.c | 60
++++++++++++++++++++++++++-------------
target/riscv/pmp.h | 3 +-4 files changed, 47 insertions(+), 42 deletions(-)
Quoted 14 lines · LIU Zhiwei
Quoted 12 lines · Weiwei Li
Quoted 9 lines · LIU Zhiwei
Quoted 1 line · Weiwei Li
This patchset tries to fix the PMP bypass problem issue https://gitlab.com/qemu-project/qemu/-/issues/1542
Please add your analysis of this issue here.
By the way, I think this problem is introduced by
https://www.mail-archive.com/[email protected]/msg939331.html
Quoted 8 lines
Quoted 5 lines
It seems have no relationship with this commit.
I think there are several problems for this issue:
1. TLB will not be cached only when the access address have matched PMP entry.
TLB will be filled only when PMP check and PTW check pass.
Quoted 1 line
So the other address access may hit the TLB(if first access of the page didn't hit the PMP entry)
This page will not be filled to TLB if the first access of the page didn't pass the PMP check.
I have given an example for this bypass in the replied email of patch 1.
Regards,
Weiwei Li
Quoted 44 lines
Quoted 2 lines
and bypass the pmp check. This is fixed by patch 1.
Never it should be.
Zhiwei
Quoted 37 lines
2. Writing to pmpaddr didn't trigger tlb flush. This is fixed by patch 3.
3. The tb isn't flushed when PMP permission changes, so It also may hit the tb and bypass the changed PMP check for instruction fetch. This is fixed by patch 5.
4. We set the
tlb_sizeto 1 to make theTLB_INVALID_MASKset. However this flag will be cleared afterfill_tlb, and this will make the host address be cached, and let the following instruction fetch in the same tb bypass the PMP check. This is fixed by patch 6.Regards,
Weiwei Li
Quoted 26 lines
I have commented on how to correct this patch. But by accident, it has been merged.
Zhiwei
Quoted 21 lines
The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-pmp-fixWeiwei Li (6):
target/riscv: Updatepmp_get_tlb_size()target/riscv: Movepmp_get_tlb_sizeapart fromget_physical_address_pmp
target/riscv: flush tlb when pmpaddr is updated target/riscv: Flush TLB only when pmpcfg/pmpaddr really changes target/riscv: flush tb when PMP entry changes accel/tcg: RemainTLB_INVALID_MASKin the address when TLB is re-filledaccel/tcg/cputlb.c | 7 -----
target/riscv/cpu_helper.c| 19 ++++---------
target/riscv/pmp.c | 60
++++++++++++++++++++++++++-------------
target/riscv/pmp.h | 3 +-4 files changed, 47 insertions(+), 42 deletions(-)