Skip to contentSkip to conversation

lib: sbi_hart: Retrun zero lenght of mhpmcounter when probing failed

RFC PATCH Applied Evgenii Prokopiev 2 participants 2 messages Sep 10 – Oct 27

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 lib: sbi_hart: Retrun zero lenght of mhpmcounter when probing failed

If writing access is not allowed, it will raise a trap, but we increase the number of allowed bits to 32 in this approach, which isn't right. There must be a write of 0.

Move the operation of increasing allowed bits into the 'if' condition.

---
 lib/sbi/sbi_hart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_hart.c +4 -2
index bee88557..3a9b6c8f 100644
@@ -450,16 +450,18 @@ static int hart_mhpm_get_allowed_bits(void)
 		val = csr_read_allowed(CSR_MHPMCOUNTER3, &trap);
 		if (trap.cause)
 			return 0;
+
+		num_bits = sbi_fls(val) + 1;
 	}
-	num_bits = sbi_fls(val) + 1;
 #if __riscv_xlen == 32
 	csr_write_allowed(CSR_MHPMCOUNTER3H, &trap, val);
 	if (!trap.cause) {
 		val = csr_read_allowed(CSR_MHPMCOUNTER3H, &trap);
 		if (trap.cause)
 			return num_bits;
+
+		num_bits += sbi_fls(val) + 1;
 	}
-	num_bits += sbi_fls(val) + 1;
 
 #endif
 
Signature · 7 lines
2.50.1 (Apple Git-155)


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/1 Applied
#1 lore raw
Quoted 8 lines · Evgenii Prokopiev

If writing access is not allowed, it will raise a trap, but we increase the number of allowed bits to 32 in this approach, which isn't right. There must be a write of 0.

Move the operation of increasing allowed bits into the 'if' condition.

Good catch!

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 37 lines
---
 lib/sbi/sbi_hart.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_hart.c +4 -3
index bee88557..3a9b6c8f 100644
@@ -450,16 +450,18 @@ static int hart_mhpm_get_allowed_bits(void)
                val = csr_read_allowed(CSR_MHPMCOUNTER3, &trap);
                if (trap.cause)
                        return 0;
+
+               num_bits = sbi_fls(val) + 1;
        }
-       num_bits = sbi_fls(val) + 1;
 #if __riscv_xlen == 32
        csr_write_allowed(CSR_MHPMCOUNTER3H, &trap, val);
        if (!trap.cause) {
                val = csr_read_allowed(CSR_MHPMCOUNTER3H, &trap);
                if (trap.cause)
                        return num_bits;
+
+               num_bits += sbi_fls(val) + 1;
        }
-       num_bits += sbi_fls(val) + 1;
 
 #endif
 
--

2.50.1 (Apple Git-155)

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi