Skip to contentSkip to conversation

miscellaneous about sbi_dtbr

PATCH Superseded Xiang W 2 participants 11 messages Apr 23, 2024 – May 9, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

v2 cover miscellaneous about sbi_dtbr

v2 changes:
- fix shmem_base checking in PATCH2

Xiang W (5):
lib: sbi: fix hart_shmem_base
lib: sbi: Remove unnecessary sbi_dbtr_shmem_disabled
lib: sbi: Removal unnecessary check dbtr_thishart_state_ptr
lib: sbi: fix DBTR_SHMEM_MAKE_PHYS for RV64
lib: sbi: Fixed memory permission check in sbi_dbtr_setup_shmem

 lib/sbi/sbi_dbtr.c | 40 ++++++++++++++--------------------------
 1 file changed, 14 insertions(+), 26 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/5 lib: sbi: fix hart_shmem_base

When only phys_hi is equal to SBI_DBTR_SHMEM_INVALID_ADDR, it may be a legal address. The old code would modify the legal address.

---
 lib/sbi/sbi_dbtr.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +4 -7
index 7142fed..5b07285 100644
@@ -84,21 +84,18 @@ static inline void sbi_dbtr_disable_shmem(void)
 
 static inline void *hart_shmem_base(void)
 {
-	struct sbi_dbtr_shmem* shmem;
 	unsigned long phys_hi, phys_lo;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
 	hs = dbtr_get_hart_state_ptr(sbi_scratch_thishart_ptr());
-
 	if (!hs)
 		return NULL;
 
-	shmem = &hs->shmem;
+	phys_hi = hs->shmem.phys_hi;
+	phys_lo = hs->shmem.phys_lo;
 
-	phys_hi = (shmem->phys_hi == SBI_DBTR_SHMEM_INVALID_ADDR
-		   ? shmem->phys_hi : 0);
-	phys_lo = (shmem->phys_lo == SBI_DBTR_SHMEM_INVALID_ADDR
-		   ? 0 : shmem->phys_lo);
+	if (phys_hi == SBI_DBTR_SHMEM_INVALID_ADDR && phys_hi == phys_lo)
+		return NULL;
 
 	return ((void *)(unsigned long)DBTR_SHMEM_MAKE_PHYS(phys_hi, phys_lo));
 }
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 1/5 lib: sbi: fix hart_shmem_base

Quoted 28 lines · Xiang W

When only phys_hi is equal to SBI_DBTR_SHMEM_INVALID_ADDR, it may be a legal address. The old code would modify the legal address.

---
  lib/sbi/sbi_dbtr.c | 11 ++++-------
  1 file changed, 4 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +2 -3
index 7142fed..5b07285 100644
@@ -84,21 +84,18 @@ static inline void sbi_dbtr_disable_shmem(void)
 
  static inline void *hart_shmem_base(void)
  {
-	struct sbi_dbtr_shmem* shmem;
  	unsigned long phys_hi, phys_lo;
  	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
  	hs = dbtr_get_hart_state_ptr(sbi_scratch_thishart_ptr());
-
  	if (!hs)
  		return NULL;
 
-	shmem = &hs->shmem;
+	phys_hi = hs->shmem.phys_hi;
+	phys_lo = hs->shmem.phys_lo;

lib/sbi/sbi_dbtr.c static inline void sbi_dbtr_disable_shmem(void) · line 101

Quoted 10 lines
lib/sbi/sbi_dbtr.c +2 -4
 
-	phys_hi = (shmem->phys_hi == SBI_DBTR_SHMEM_INVALID_ADDR
-		   ? shmem->phys_hi : 0);
-	phys_lo = (shmem->phys_lo == SBI_DBTR_SHMEM_INVALID_ADDR
-		   ? 0 : shmem->phys_lo);
+	if (phys_hi == SBI_DBTR_SHMEM_INVALID_ADDR && phys_hi == phys_lo)
+		return NULL;
 
  	return ((void *)(unsigned long)DBTR_SHMEM_MAKE_PHYS(phys_hi, phys_lo));
  }

LGTM

author
#3 lore raw

PATCH v2 2/5 lib: sbi: Remove unnecessary sbi_dbtr_shmem_disabled

In reply to Xiang W

The code of hart_shmem_base is similar to sbi_dbtr_shmem_disabled. When hart_shmem_base is used, sbi_dbtr_shmem_disabled can be removed.

---
 lib/sbi/sbi_dbtr.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +6 -7
index 5b07285..ceff185 100644
@@ -537,10 +537,9 @@ int sbi_dbtr_read_trig(unsigned long smode,
 	    trig_idx_base + trig_count >= hs->total_trigs)
 		return SBI_ERR_INVALID_PARAM;
 
-	if (sbi_dbtr_shmem_disabled())
-		return SBI_ERR_NO_SHMEM;
-
 	shmem_base = hart_shmem_base();
+	if (shmem_base == NULL)
+		return SBI_ERR_NO_SHMEM;
 
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
 		sbi_hart_map_saddr((unsigned long)entry, sizeof(*entry));
@@ -567,10 +566,10 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
-	if (sbi_dbtr_shmem_disabled())
+	shmem_base = hart_shmem_base();
+	if (shmem_base == NULL)
 		return SBI_ERR_NO_SHMEM;
 
-	shmem_base = hart_shmem_base();
 	hs = dbtr_thishart_state_ptr();
 
 	/* Check requested triggers configuration */
@@ -679,10 +678,10 @@ int sbi_dbtr_update_trig(unsigned long smode,
 	void *shmem_base = NULL;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
-	if (sbi_dbtr_shmem_disabled())
+	shmem_base = hart_shmem_base();
+	if (shmem_base == NULL)
 		return SBI_ERR_NO_SHMEM;
 
-	shmem_base = hart_shmem_base();
 	hs = dbtr_thishart_state_ptr();
 	if (!hs)
 		return SBI_ERR_FAILED;
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 2/5 lib: sbi: Remove unnecessary sbi_dbtr_shmem_disabled

Quoted 42 lines · Xiang W

The code of hart_shmem_base is similar to sbi_dbtr_shmem_disabled. When hart_shmem_base is used, sbi_dbtr_shmem_disabled can be removed.

---
  lib/sbi/sbi_dbtr.c | 13 ++++++-------
  1 file changed, 6 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +4 -5
index 5b07285..ceff185 100644
@@ -537,10 +537,9 @@ int sbi_dbtr_read_trig(unsigned long smode,
  	    trig_idx_base + trig_count >= hs->total_trigs)
  		return SBI_ERR_INVALID_PARAM;
 
-	if (sbi_dbtr_shmem_disabled())
-		return SBI_ERR_NO_SHMEM;
-
  	shmem_base = hart_shmem_base();
+	if (shmem_base == NULL)
+		return SBI_ERR_NO_SHMEM;
 
  	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
  		sbi_hart_map_saddr((unsigned long)entry, sizeof(*entry));
@@ -567,10 +566,10 @@ int sbi_dbtr_install_trig(unsigned long smode,
  	struct sbi_dbtr_trigger *trig;
  	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
-	if (sbi_dbtr_shmem_disabled())
+	shmem_base = hart_shmem_base();
+	if (shmem_base == NULL)
  		return SBI_ERR_NO_SHMEM;
 
-	shmem_base = hart_shmem_base();
  	hs = dbtr_thishart_state_ptr();
 
  	/* Check requested triggers configuration */
@@ -679,10 +678,10 @@ int sbi_dbtr_update_trig(unsigned long smode,
  	void *shmem_base = NULL;
  	struct sbi_dbtr_hart_triggers_state *hs = NULL;

lib/sbi/sbi_dbtr.c int sbi_dbtr_update_trig(unsigned long smode, · line 687

Quoted 10 lines
lib/sbi/sbi_dbtr.c +2 -2
 
-	if (sbi_dbtr_shmem_disabled())
+	shmem_base = hart_shmem_base();
+	if (shmem_base == NULL)
  		return SBI_ERR_NO_SHMEM;
 
-	shmem_base = hart_shmem_base();
  	hs = dbtr_thishart_state_ptr();
  	if (!hs)
  		return SBI_ERR_FAILED;

I would still keep the sbi_dbtr_shmem_disabled() for brevity of usage. Better, change sbi_dbtr_shmem_disabled() code to do this check and return true/false.

Regards
Himanshu

author
#5 lore raw

PATCH v2 3/5 lib: sbi: Removal unnecessary check dbtr_thishart_state_ptr

In reply to Xiang W

After getting hart_shmem_base, dbtr_thishart_state_ptr cannot be empty. So remove check code.

---
 lib/sbi/sbi_dbtr.c | 2 --
 1 file changed, 2 deletions(-)
lib/sbi/sbi_dbtr.c +0 -2
index ceff185..4093a82 100644
@@ -683,8 +683,6 @@ int sbi_dbtr_update_trig(unsigned long smode,
 		return SBI_ERR_NO_SHMEM;
 
 	hs = dbtr_thishart_state_ptr();
-	if (!hs)
-		return SBI_ERR_FAILED;
 
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 3/5 lib: sbi: Removal unnecessary check dbtr_thishart_state_ptr

Quoted 13 lines · Xiang W

After getting hart_shmem_base, dbtr_thishart_state_ptr cannot be empty. So remove check code.

---
  lib/sbi/sbi_dbtr.c | 2 --
  1 file changed, 2 deletions(-)
lib/sbi/sbi_dbtr.c +0 -0
index ceff185..4093a82 100644

lib/sbi/sbi_dbtr.c int sbi_dbtr_update_trig(unsigned long smode, · line 688

Quoted 8 lines
lib/sbi/sbi_dbtr.c +0 -2
@@ -683,8 +683,6 @@ int sbi_dbtr_update_trig(unsigned long smode,
  		return SBI_ERR_NO_SHMEM;
 
  	hs = dbtr_thishart_state_ptr();
-	if (!hs)
-		return SBI_ERR_FAILED;
 
  	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
  		trig = INDEX_TO_TRIGGER(idx);

LGTM

- Himanshu

author
#7 lore raw

PATCH v2 4/5 lib: sbi: fix DBTR_SHMEM_MAKE_PHYS for RV64

In reply to Xiang W

Obtaining a 64-bit address under rv64 does not require combining 32-bit registers

---
 lib/sbi/sbi_dbtr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_dbtr.c +1 -1
index 4093a82..29be9bb 100644
@@ -48,7 +48,7 @@ static unsigned long hart_state_ptr_offset;
 	     _idx++, _entry = ((_etype *)_base + _idx))
 
 #if __riscv_xlen == 64
-#define DBTR_SHMEM_MAKE_PHYS(_p_hi, _p_lo) (((u64)(_p_hi) << 32) | (_p_lo))
+#define DBTR_SHMEM_MAKE_PHYS(_p_hi, _p_lo) (_p_lo)
 #elif __riscv_xlen == 32
 #define DBTR_SHMEM_MAKE_PHYS(_p_hi, _p_lo) (((u64)(_p_hi) << 32) | (_p_lo))
 #else
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 4/5 lib: sbi: fix DBTR_SHMEM_MAKE_PHYS for RV64

Quoted 13 lines · Xiang W

Obtaining a 64-bit address under rv64 does not require combining 32-bit registers

---
  lib/sbi/sbi_dbtr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_dbtr.c +0 -0
index 4093a82..29be9bb 100644

lib/sbi/sbi_dbtr.c static unsigned long hart_state_ptr_offset; · line 54

Quoted 8 lines
lib/sbi/sbi_dbtr.c +1 -1
@@ -48,7 +48,7 @@ static unsigned long hart_state_ptr_offset;
  	     _idx++, _entry = ((_etype *)_base + _idx))
 
  #if __riscv_xlen == 64
-#define DBTR_SHMEM_MAKE_PHYS(_p_hi, _p_lo) (((u64)(_p_hi) << 32) | (_p_lo))
+#define DBTR_SHMEM_MAKE_PHYS(_p_hi, _p_lo) (_p_lo)
  #elif __riscv_xlen == 32
  #define DBTR_SHMEM_MAKE_PHYS(_p_hi, _p_lo) (((u64)(_p_hi) << 32) | (_p_lo))
  #else

LGTM

Regards
Himanshu

author
#9 lore raw

PATCH v2 5/5 lib: sbi: Fixed memory permission check in sbi_dbtr_setup_shmem

In reply to Xiang W

The previous code detected shmem_phys_hi and shmem_phys_lo as two addresses. fix this bug

---
 lib/sbi/sbi_dbtr.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
lib/sbi/sbi_dbtr.c +3 -9
index 29be9bb..f1b53e9 100644
@@ -293,17 +293,11 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode,
 	if (shmem_phys_lo & SBI_DBTR_SHMEM_ALIGN_MASK)
 		return SBI_ERR_INVALID_PARAM;
 
-	if (dom && !sbi_domain_check_addr(dom, shmem_phys_lo, smode,
-					  SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
+	if (dom && !sbi_domain_check_addr(dom,
+		  DBTR_SHMEM_MAKE_PHYS(shmem_phys_hi, shmem_phys_lo), smode,
+		  SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
 		return SBI_ERR_INVALID_ADDRESS;
 
-	if (shmem_phys_hi != SBI_DBTR_SHMEM_INVALID_ADDR) {
-		if (dom &&
-		    !sbi_domain_check_addr(dom, shmem_phys_hi, smode,
-					   SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
-			return SBI_ERR_INVALID_ADDRESS;
-	}
-
 	hart_state = dbtr_thishart_state_ptr();
 	if (!hart_state)
 		return SBI_ERR_FAILED;
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 5/5 lib: sbi: Fixed memory permission check in sbi_dbtr_setup_shmem

Quoted 23 lines · Xiang W

The previous code detected shmem_phys_hi and shmem_phys_lo as two addresses. fix this bug

---
  lib/sbi/sbi_dbtr.c | 12 +++---------
  1 file changed, 3 insertions(+), 9 deletions(-)
lib/sbi/sbi_dbtr.c +3 -2
index 29be9bb..f1b53e9 100644
@@ -293,17 +293,11 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode,
  	if (shmem_phys_lo & SBI_DBTR_SHMEM_ALIGN_MASK)
  		return SBI_ERR_INVALID_PARAM;
 
-	if (dom && !sbi_domain_check_addr(dom, shmem_phys_lo, smode,
-					  SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
+	if (dom && !sbi_domain_check_addr(dom,
+		  DBTR_SHMEM_MAKE_PHYS(shmem_phys_hi, shmem_phys_lo), smode,
+		  SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
  		return SBI_ERR_INVALID_ADDRESS;
 

lib/sbi/sbi_dbtr.c int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode, · line 303

Quoted 10 lines
lib/sbi/sbi_dbtr.c +0 -7
-	if (shmem_phys_hi != SBI_DBTR_SHMEM_INVALID_ADDR) {
-		if (dom &&
-		    !sbi_domain_check_addr(dom, shmem_phys_hi, smode,
-					   SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))
-			return SBI_ERR_INVALID_ADDRESS;
-	}
-
  	hart_state = dbtr_thishart_state_ptr();
  	if (!hart_state)
  		return SBI_ERR_FAILED;

LGTM

Regards
Himanshu