Skip to contentSkip to conversation

lib: sbi: Fix PMP address bits detection

PATCH New Anup Patel 5 participants 9 messages Nov 8, 2021 – Nov 11, 2021

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

---
 lib/sbi/sbi_hart.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_hart.c +4 -0
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long hart_pmp_get_allowed_addr(void)
 	unsigned long val = 0;
 	struct sbi_trap_info trap = {0};
 
+	csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+	if (trap.cause)
+		return 0;
+
 	csr_write_allowed(CSR_PMPADDR0, (ulong)&trap, PMP_ADDR_MASK);
 	if (!trap.cause) {
 		val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

Quoted 33 lines · Anup Patel

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

---
 lib/sbi/sbi_hart.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_hart.c +4 -1
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long hart_pmp_get_allowed_addr(void)
        unsigned long val = 0;
        struct sbi_trap_info trap = {0};
 
+       csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+       if (trap.cause)
+               return 0;
+
        csr_write_allowed(CSR_PMPADDR0, (ulong)&trap, PMP_ADDR_MASK);
        if (!trap.cause) {
                val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);
--

2.25.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 2 lines
Regards,
Atish
#2 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

In reply to Anup Patel

? 2021-11-08???? 10:36 +0530?Anup Patel???

Quoted 4 lines

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Why? Disable PMP? Prevent the generated PMP from affecting program execution? If so, I think the PMP configuration should be backed up first, then tested, and finally restored

Regards,
Xiang W

Quoted 29 lines
Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

---
?lib/sbi/sbi_hart.c | 4 ++++
?1 file changed, 4 insertions(+)

lib/sbi/sbi_hart.c +0 -0
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long
 hart_pmp_get_allowed_addr(void)
 ????????unsigned long val = 0;
 ????????struct sbi_trap_info trap = {0};

?
+???????csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0); +???????if (trap.cause)
+???????????????return 0;
+
????????csr_write_allowed(CSR_PMPADDR0, (ulong)&trap, PMP_ADDR_MASK); ????????if (!trap.cause) {
????????????????val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);

Signature · 1 line
2.25.1
#3 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

Quoted 9 lines · Xiang W

? 2021-11-08???? 10:36 +0530?Anup Patel???

Quoted 4 lines

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Why? Disable PMP? Prevent the generated PMP from affecting program execution? If so, I think the PMP configuration should be backed up first, then tested, and finally restored

We don't need to save/restore PMP configuration here because it is very early during HART feature detection.

For the rest refer,
https://github.com/riscv-software-src/opensbi/issues/225

Regards,
Anup

Quoted 34 lines

Regards,
Xiang W

Quoted 29 lines
Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

---
 lib/sbi/sbi_hart.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_hart.c +4 -0
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long
 hart_pmp_get_allowed_addr(void)
        unsigned long val = 0;
        struct sbi_trap_info trap = {0};
 
+       csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+       if (trap.cause)
+               return 0;
+
        csr_write_allowed(CSR_PMPADDR0, (ulong)&trap, PMP_ADDR_MASK);
        if (!trap.cause) {
                val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);

--
2.25.1

#4 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

? 2021-11-11???? 18:38 +0530?Anup Patel???

Quoted 11 lines · Xiang W
Quoted 9 lines

? 2021-11-08???? 10:36 +0530?Anup Patel???

Quoted 4 lines

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Why? Disable PMP? Prevent the generated PMP from affecting program execution? If so, I think the PMP configuration should be backed up first, then tested, and finally restored

Quoted 8 lines

We don't need to save/restore PMP configuration here because it is very early during HART feature detection.

For the rest refer,
https://github.com/riscv-software-src/opensbi/issues/225

Regards,
Anup

This code will be executed after each warm boot, pmpcfg0 may be locked, and writing 0 may not work. I suggest to make this code execute once through a static variable

Regards,
Xiang W

Quoted 37 lines
Quoted 37 lines

Regards,
Xiang W

Quoted 31 lines
Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

---
?lib/sbi/sbi_hart.c | 4 ++++
?1 file changed, 4 insertions(+)

lib/sbi/sbi_hart.c +4 -0
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long
 hart_pmp_get_allowed_addr(void)
 ??????? unsigned long val = 0;
 ??????? struct sbi_trap_info trap = {0};
 
+?????? csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+?????? if (trap.cause)
+?????????????? return 0;
+
 ??????? csr_write_allowed(CSR_PMPADDR0, (ulong)&trap,
 PMP_ADDR_MASK);

??????? if (!trap.cause) {
??????????????? val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);
--
2.25.1

#5 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

Quoted 19 lines · Xiang W

? 2021-11-11???? 18:38 +0530?Anup Patel???

Quoted 17 lines · Xiang W
Quoted 9 lines

? 2021-11-08???? 10:36 +0530?Anup Patel???

Quoted 4 lines

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Why? Disable PMP? Prevent the generated PMP from affecting program execution? If so, I think the PMP configuration should be backed up first, then tested, and finally restored

We don't need to save/restore PMP configuration here because it is very early during HART feature detection.

For the rest refer,
https://github.com/riscv-software-src/opensbi/issues/225

Quoted 6 lines
Quoted 2 lines

Regards,
Anup

This code will be executed after each warm boot, pmpcfg0 may be locked, and writing 0 may not work. I suggest to make this code execute once through a static variable

No need for a static variable, we can just check "hfeatures->pmp_addr_bits" to be zero before calling hart_pmp_get_allowed_addr().

Regards,
Anup

Quoted 42 lines

Regards,
Xiang W

Quoted 37 lines
Quoted 37 lines

Regards,
Xiang W

Quoted 31 lines
Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

---
 lib/sbi/sbi_hart.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_hart.c +4 -0
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long
 hart_pmp_get_allowed_addr(void)
        unsigned long val = 0;
        struct sbi_trap_info trap = {0};
 
+       csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+       if (trap.cause)
+               return 0;
+
        csr_write_allowed(CSR_PMPADDR0, (ulong)&trap,
 PMP_ADDR_MASK);

if (!trap.cause) {
val = csr_read_allowed(CSR_PMPADDR0,
(ulong)&trap);
--
2.25.1

#6 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

? 2021-11-11???? 21:01 +0530?Anup Patel???

Quoted 33 lines · Xiang W
Quoted 23 lines

? 2021-11-11???? 18:38 +0530?Anup Patel???

Quoted 21 lines · Xiang W
Quoted 4 lines

? 2021-11-08???? 10:36 +0530?Anup Patel???

Quoted 2 lines

From: Vasan VS <[email protected]>

Quoted 8 lines
Quoted 3 lines

We should ensure that pmpcfg0.pmp0cfg is set to zero before using
pmpaddr0 CSR for detecting implemented PMP address bits.

Why? Disable PMP? Prevent the generated PMP from affecting program
execution? If so, I think the PMP configuration should be backed up
first, then tested, and finally restored

We don't need to save/restore PMP configuration here because it is
very early during HART feature detection.

For the rest refer,
https://github.com/riscv-software-src/opensbi/issues/225

Quoted 8 lines
Quoted 2 lines

Regards,
Anup

This code will be executed after each warm boot, pmpcfg0 may be locked,
and writing 0 may not work. I suggest to make this code execute once
through a static variable

Quoted 4 lines

No need for a static variable, we can just check "hfeatures-

Quoted 1 line

pmp_addr_bits"

to be zero before calling hart_pmp_get_allowed_addr().

Yes, good idea

Regards,
Xiang W

Quoted 47 lines

Regards,
Anup

Quoted 43 lines

Regards,
Xiang W

Quoted 38 lines
Quoted 38 lines

Regards,
Xiang W

Quoted 32 lines
Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and

number
of address bits")

---
?lib/sbi/sbi_hart.c | 4 ++++
?1 file changed, 4 insertions(+)

lib/sbi/sbi_hart.c +4 -0
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long
 hart_pmp_get_allowed_addr(void)
 ??????? unsigned long val = 0;
 ??????? struct sbi_trap_info trap = {0};
 
+?????? csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+?????? if (trap.cause)
+?????????????? return 0;
+
 ??????? csr_write_allowed(CSR_PMPADDR0, (ulong)&trap,
 PMP_ADDR_MASK);

??????? if (!trap.cause) {
??????????????? val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);
--
2.25.1

#7 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

In reply to Anup Patel
Quoted 10 lines · anup patel anup.patel at wdc.com

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

---

Looks good to me.

Quoted 26 lines

lib/sbi/sbi_hart.c | 4 ++++

1 file changed, 4 insertions(+)
lib/sbi/sbi_hart.c +4 -1
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long hart_pmp_get_allowed_addr(void)
 	unsigned long val = 0;
 	struct sbi_trap_info trap = {0};
 
+	csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+	if (trap.cause)
+		return 0;
+
 	csr_write_allowed(CSR_PMPADDR0, (ulong)&trap, PMP_ADDR_MASK);
 	if (!trap.cause) {
 		val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);
--

2.25.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Applied
#8 lore raw

PATCH 1/1 lib: sbi: Fix PMP address bits detection

In reply to Anup Patel
Quoted 10 lines · Anup Patel

From: Vasan VS <[email protected]>

We should ensure that pmpcfg0.pmp0cfg is set to zero before using pmpaddr0 CSR for detecting implemented PMP address bits.

Fixes bf21632860b4 ("lib: sbi: Detect PMP granularity and number

of address bits")

Applied this patch to the riscv/opensbi repo

Regards,
Anup

Quoted 22 lines
---
 lib/sbi/sbi_hart.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_hart.c +4 -1
index 8eb0c38..366066b 100644
@@ -339,6 +339,10 @@ static unsigned long hart_pmp_get_allowed_addr(void)
        unsigned long val = 0;
        struct sbi_trap_info trap = {0};
 
+       csr_write_allowed(CSR_PMPCFG0, (ulong)&trap, 0);
+       if (trap.cause)
+               return 0;
+
        csr_write_allowed(CSR_PMPADDR0, (ulong)&trap, PMP_ADDR_MASK);
        if (!trap.cause) {
                val = csr_read_allowed(CSR_PMPADDR0, (ulong)&trap);
--

2.25.1