Skip to contentSkip to conversation

dbtr: Fixes and heterogeneous trigger types

PATCH In review Nicholas Piggin 4 participants 30 messages Mar 13 – Sep 21

lore
Share & tools
lore
#0 lore raw

cover dbtr: Fixes and heterogeneous trigger types

In the course of implementing and testing heterogeneous trigger types as found in the Tenstorrent Ascalon CPU, I found some issues to fix as well.

Patches 1-11 contain fixes and improvements for existing function, and make most of the new kvm-unit-tests tests pass. 12-17 implement "heterogenous" triggers where different HW triggers support different functions so we have to relax the 1:1 mapping from DBTR trig to HW trigger index and allocate HW triggers independently from what can satisfy the requested type. Patch 18 is another possible fix but it relies on update to SBI spec so I put it last. I can move it ahead into the fixes block if the issue is confirmed.

Thanks,
Nick

Nicholas Piggin (18):
dbtr: Add consistent range checks to trigger ecalls
dbtr: Trigger update should set sbiret.value on failure
dbtr: Fix endian conversion in trigger install handler
dbtr: Return correct error on install not supported
dbtr: Do not support chain bit
dbtr: Improve trigger update error checking
dbtr: Check for invalid and unsupported triggers in update
dbtr: Improve error handling for trigger enable, disable, uninstall
dbtr: Read triggers should not read HW trigger if not mapped
dbtr: Avoid crash in sbi_debug_read_triggers
dbtr: Succeed operations with no triggers in mask
dbtr: Move hardware trigger probing to a function
dbtr: Rework install and update error handling
dbtr: Decouple dbtr trigger index from hardware trigger number
dbtr: Move trigger feature support test into a function
dbtr: Heterogeneous trigger type support
dbtr: Heterogeneous access type matching for mcontrol triggers
dbtr: Work around specification bug in range checks

 include/sbi/sbi_dbtr.h   |  17 +-
 lib/sbi/sbi_dbtr.c       | 499 ++++++++++++++++++++++++++++++---------
 lib/sbi/sbi_ecall_dbtr.c |   2 +-
 3 files changed, 407 insertions(+), 111 deletions(-)
Signature · 7 lines
2.51.0


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

PATCH 1/18 dbtr: Add consistent range checks to trigger ecalls

Be more consistent with the sequence of trigger range checks in dbtr ecall handlers. This includes adding a missing range check in install triggers that can result in SBI past the end of the shm region.

---
 lib/sbi/sbi_dbtr.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
lib/sbi/sbi_dbtr.c +24 -3
index 8bcb4312..b0160163 100644
@@ -550,6 +550,9 @@ int sbi_dbtr_read_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_count >= hs->total_trigs)
+		return SBI_ERR_BAD_RANGE;
+
 	if (trig_idx_base >= hs->total_trigs ||
 	    trig_idx_base + trig_count >= hs->total_trigs)
 		return SBI_ERR_INVALID_PARAM;
@@ -594,6 +597,9 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_count >= hs->total_trigs)
+		return SBI_ERR_BAD_RANGE;
+
 	if (sbi_dbtr_shmem_disabled(hs))
 		return SBI_ERR_NO_SHMEM;
 
@@ -663,6 +669,11 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_idx_base >= hs->total_trigs ||
+		find_last_bit(&trig_idx_mask, hs->total_trigs) +
+				trig_idx_base >= hs->total_trigs)
+		return SBI_ERR_INVALID_PARAM;
+
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
 		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
@@ -688,6 +699,11 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_idx_base >= hs->total_trigs ||
+		find_last_bit(&trig_idx_mask, hs->total_trigs) +
+				trig_idx_base >= hs->total_trigs)
+		return SBI_ERR_INVALID_PARAM;
+
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
 		sbi_dprintf("%s: enable trigger %lu\n", __func__, idx);
@@ -710,14 +726,14 @@ int sbi_dbtr_update_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_count >= hs->total_trigs)
+		return SBI_ERR_BAD_RANGE;
+
 	if (sbi_dbtr_shmem_disabled(hs))
 		return SBI_ERR_NO_SHMEM;
 
 	shmem_base = hart_shmem_base(hs);
 
-	if (trig_count >= hs->total_trigs)
-		return SBI_ERR_BAD_RANGE;
-
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
 		sbi_hart_protection_map_range((unsigned long)entry, sizeof(*entry));
 		trig_idx = entry->id.idx;
@@ -754,6 +770,11 @@ int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_idx_base >= hs->total_trigs ||
+		find_last_bit(&trig_idx_mask, hs->total_trigs) +
+				trig_idx_base >= hs->total_trigs)
+		return SBI_ERR_INVALID_PARAM;
+
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
 		dbtr_trigger_disable(trig);
Signature · 7 lines
2.51.0


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

Re: [PATCH 01/18] dbtr: Add consistent range checks to trigger ecalls

Hi, Nicholas
This is a significant improvement over the previous version!

I have a few comments below, though they may be off the mark since they're based on my own reading of the SBI and Sdtrig specifications.

lib/sbi/sbi_dbtr.c int sbi_dbtr_install_trig(unsigned long smode, · line 601

Quoted 6 lines · Nicholas Piggin
lib/sbi/sbi_dbtr.c +2 -0
@@ -594,6 +597,9 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_count >= hs->total_trigs)
+		return SBI_ERR_BAD_RANGE;

According to the SBI specification (section 19.4):
- "The sbiret.value is set to zero upon success or if shared memory is disabled". I interpret this to mean that sbi_dbtr_install_trig must return a trigger index along with an SBI_ERR_BAD_RANGE error.

- "sbiret.value is set to the array index i of the failing
trigger configuration". In this case, sbiret.value should be hs->total_trigs, because that is the index of the first failing trigger configuration.

Same comment applies to sbi_dbtr_update_trig.

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

Quoted 7 lines
lib/sbi/sbi_dbtr.c +3 -0
@@ -710,14 +726,14 @@ int sbi_dbtr_update_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_count >= hs->total_trigs)
+		return SBI_ERR_BAD_RANGE;
+

Regards
Ilya

Quoted 4 lines
(quoted diff) +0 -1
-- 

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
#3 lore raw

Re: [PATCH 01/18] dbtr: Add consistent range checks to trigger ecalls

Quoted 5 lines · Ilya Mamay

Hi, Nicholas
This is a significant improvement over the previous version!

I have a few comments below, though they may be off the mark since they're based on my own reading of the SBI and Sdtrig specifications.

Hey Ilya, thanks for the review, sorry to take a while to get back to it I was on vacation for a few weeks.

Quoted 13 lines · Nicholas Piggin

lib/sbi/sbi_dbtr.c int sbi_dbtr_install_trig(unsigned long smode, · line 601

Quoted 6 lines
lib/sbi/sbi_dbtr.c +2 -0
@@ -594,6 +597,9 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (trig_count >= hs->total_trigs)
+		return SBI_ERR_BAD_RANGE;

According to the SBI specification (section 19.4):
- "The sbiret.value is set to zero upon success or if shared memory is disabled". I interpret this to mean that sbi_dbtr_install_trig must return a trigger index along with an SBI_ERR_BAD_RANGE error.

Quoted 5 lines

- "sbiret.value is set to the array index i of the failing
trigger configuration". In this case, sbiret.value should be hs->total_trigs, because that is the index of the first failing trigger configuration.

Same comment applies to sbi_dbtr_update_trig.

Shapr observation. I think it may be technically ambiguous, but I felt the intention was the other way. That is, bad range is not any particular trigger failing, but an overall error before we get to individual trigger processing.

sbi_debug_read_triggers() can return SBI_ERR_BAD_RANGE too for example, and that could not point to a bad trigger.

I would prefer to leave as is unless there is strong opinion or requirement for now, since that is leaving existing behaviour unchanged.

Thanks,
Nick

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/18
#4 lore raw

Re: [PATCH 01/18] dbtr: Add consistent range checks to trigger ecalls

In reply to Nicholas Piggin
Quoted 78 lines · Nicholas Piggin

Be more consistent with the sequence of trigger range checks in dbtr ecall handlers. This includes adding a missing range check in install triggers that can result in SBI past the end of the shm region.

---
 lib/sbi/sbi_dbtr.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
lib/sbi/sbi_dbtr.c +19 -3
index 8bcb4312..b0160163 100644
@@ -550,6 +550,9 @@ int sbi_dbtr_read_trig(unsigned long smode,
        if (!hs)
                return SBI_ERR_FAILED;
 
+       if (trig_count >= hs->total_trigs)
+               return SBI_ERR_BAD_RANGE;
+
        if (trig_idx_base >= hs->total_trigs ||
            trig_idx_base + trig_count >= hs->total_trigs)
                return SBI_ERR_INVALID_PARAM;
@@ -594,6 +597,9 @@ int sbi_dbtr_install_trig(unsigned long smode,
        if (!hs)
                return SBI_ERR_FAILED;
 
+       if (trig_count >= hs->total_trigs)
+               return SBI_ERR_BAD_RANGE;
+
        if (sbi_dbtr_shmem_disabled(hs))
                return SBI_ERR_NO_SHMEM;
 
@@ -663,6 +669,11 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
        if (!hs)
                return SBI_ERR_FAILED;
 
+       if (trig_idx_base >= hs->total_trigs ||
+               find_last_bit(&trig_idx_mask, hs->total_trigs) +
+                               trig_idx_base >= hs->total_trigs)
+               return SBI_ERR_INVALID_PARAM;
+
        for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
                trig = INDEX_TO_TRIGGER(idx);
                if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
@@ -688,6 +699,11 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
        if (!hs)
                return SBI_ERR_FAILED;
 
+       if (trig_idx_base >= hs->total_trigs ||
+               find_last_bit(&trig_idx_mask, hs->total_trigs) +
+                               trig_idx_base >= hs->total_trigs)
+               return SBI_ERR_INVALID_PARAM;
+
        for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
                trig = INDEX_TO_TRIGGER(idx);
                sbi_dprintf("%s: enable trigger %lu\n", __func__, idx);
@@ -710,14 +726,14 @@ int sbi_dbtr_update_trig(unsigned long smode,
        if (!hs)
                return SBI_ERR_FAILED;
 
+       if (trig_count >= hs->total_trigs)
+               return SBI_ERR_BAD_RANGE;
+
        if (sbi_dbtr_shmem_disabled(hs))
                return SBI_ERR_NO_SHMEM;
 
        shmem_base = hart_shmem_base(hs);
 
-       if (trig_count >= hs->total_trigs)
-               return SBI_ERR_BAD_RANGE;
-
        for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
                sbi_hart_protection_map_range((unsigned long)entry, sizeof(*entry));
                trig_idx = entry->id.idx;
@@ -754,6 +770,11 @@ int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
        if (!hs)

lib/sbi/sbi_dbtr.c int sbi_dbtr_disable_trig(unsigned long trig_idx_base, · line 780

Quoted 10 lines
lib/sbi/sbi_dbtr.c +5 -0
                return SBI_ERR_FAILED;
 
+       if (trig_idx_base >= hs->total_trigs ||
+               find_last_bit(&trig_idx_mask, hs->total_trigs) +
+                               trig_idx_base >= hs->total_trigs)
+               return SBI_ERR_INVALID_PARAM;
+
        for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
                trig = INDEX_TO_TRIGGER(idx);
                dbtr_trigger_disable(trig);

Looks good to me.

Quoted 8 lines

--
2.51.0

--
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
#5 lore raw

PATCH 2/18 dbtr: Trigger update should set sbiret.value on failure

In reply to Nicholas Piggin

According to RISC-V SBI specification, trigger updates should return the value of the problem trigger index on failure.

---
 include/sbi/sbi_dbtr.h   | 2 +-
 lib/sbi/sbi_dbtr.c       | 4 +++-
 lib/sbi/sbi_ecall_dbtr.c | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)
include/sbi/sbi_dbtr.h +1 -1
index 5e0bf84e..b8b5c9ab 100644
@@ -115,7 +115,7 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
 int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 			 unsigned long trig_idx_mask);
 int sbi_dbtr_update_trig(unsigned long smode,
-			 unsigned long trig_count);
+			 unsigned long trig_count, unsigned long *out);
 int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
 			  unsigned long trig_idx_mask);
 
lib/sbi/sbi_dbtr.c +3 -1
index b0160163..8f59e407 100644
@@ -714,7 +714,7 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 }
 
 int sbi_dbtr_update_trig(unsigned long smode,
-			 unsigned long trig_count)
+			 unsigned long trig_count, unsigned long *out)
 {
 	unsigned long trig_idx;
 	struct sbi_dbtr_trigger *trig;
@@ -740,6 +740,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 
 		if (trig_idx >= hs->total_trigs) {
 			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
+			*out = _idx;
 			return SBI_ERR_INVALID_PARAM;
 		}
 
@@ -747,6 +748,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 
 		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED))) {
 			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
+			*out = _idx;
 			return SBI_ERR_FAILED;
 		}
 
lib/sbi/sbi_ecall_dbtr.c +1 -1
index 40a437ee..f3196feb 100644
@@ -43,7 +43,7 @@ static int sbi_ecall_dbtr_handler(unsigned long extid, unsigned long funcid,
 		ret = sbi_dbtr_enable_trig(regs->a0, regs->a1);
 		break;
 	case SBI_EXT_DBTR_TRIGGER_UPDATE:
-		ret = sbi_dbtr_update_trig(smode, regs->a0);
+		ret = sbi_dbtr_update_trig(smode, regs->a0, &out->value);
 		break;
 	case SBI_EXT_DBTR_TRIGGER_DISABLE:
 		ret = sbi_dbtr_disable_trig(regs->a0, regs->a1);
Signature · 7 lines
2.51.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 2/18
#6 lore raw

Re: [PATCH 02/18] dbtr: Trigger update should set sbiret.value on failure

Quoted 57 lines · Nicholas Piggin

According to RISC-V SBI specification, trigger updates should return the value of the problem trigger index on failure.

---
 include/sbi/sbi_dbtr.h   | 2 +-
 lib/sbi/sbi_dbtr.c       | 4 +++-
 lib/sbi/sbi_ecall_dbtr.c | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)
include/sbi/sbi_dbtr.h +1 -1
index 5e0bf84e..b8b5c9ab 100644
@@ -115,7 +115,7 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
 int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 			 unsigned long trig_idx_mask);
 int sbi_dbtr_update_trig(unsigned long smode,
-			 unsigned long trig_count);
+			 unsigned long trig_count, unsigned long *out);
 int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
 			  unsigned long trig_idx_mask);
 
lib/sbi/sbi_dbtr.c +3 -1
index b0160163..8f59e407 100644
@@ -714,7 +714,7 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 }
 
 int sbi_dbtr_update_trig(unsigned long smode,
-			 unsigned long trig_count)
+			 unsigned long trig_count, unsigned long *out)
 {
 	unsigned long trig_idx;
 	struct sbi_dbtr_trigger *trig;
@@ -740,6 +740,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 
 		if (trig_idx >= hs->total_trigs) {
 			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
+			*out = _idx;
 			return SBI_ERR_INVALID_PARAM;
 		}
 
@@ -747,6 +748,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 
 		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED))) {
 			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
+			*out = _idx;
 			return SBI_ERR_FAILED;
 		}
 
lib/sbi/sbi_ecall_dbtr.c +0 -0
index 40a437ee..f3196feb 100644

lib/sbi/sbi_ecall_dbtr.c static int sbi_ecall_dbtr_handler(unsigned long extid, unsigned long funcid, · line 49

Quoted 8 lines
lib/sbi/sbi_ecall_dbtr.c +1 -1
@@ -43,7 +43,7 @@ static int sbi_ecall_dbtr_handler(unsigned long extid, unsigned long funcid,
 		ret = sbi_dbtr_enable_trig(regs->a0, regs->a1);
 		break;
 	case SBI_EXT_DBTR_TRIGGER_UPDATE:
-		ret = sbi_dbtr_update_trig(smode, regs->a0);
+		ret = sbi_dbtr_update_trig(smode, regs->a0, &out->value);
 		break;
 	case SBI_EXT_DBTR_TRIGGER_DISABLE:
 		ret = sbi_dbtr_disable_trig(regs->a0, regs->a1);

Looks good.

T/R
Himanshu

Quoted 8 lines
Signature · 7 lines
2.51.0


-- 
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
#7 lore raw

PATCH 3/18 dbtr: Fix endian conversion in trigger install handler

In reply to Nicholas Piggin

An endian conversion was missed loading tdata1 from shm. Add the conversion and change the variable name from ctrl to tdata1 while here.

---
 lib/sbi/sbi_dbtr.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
lib/sbi/sbi_dbtr.c +5 -4
index 8f59e407..10ed6b3e 100644
@@ -589,7 +589,6 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	union sbi_dbtr_shmem_entry *entry;
 	struct sbi_dbtr_data_msg *recv;
 	struct sbi_dbtr_id_msg *xmit;
-	unsigned long ctrl;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
@@ -609,17 +608,19 @@ int sbi_dbtr_install_trig(unsigned long smode,
 
 	/* Check requested triggers configuration */
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
+		unsigned long tdata1;
+
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
-		ctrl = recv->tdata1;
+		tdata1 = lle_to_cpu(recv->tdata1);
 
-		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(ctrl))) {
+		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1))) {
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
 			return SBI_ERR_FAILED;
 		}
 
-		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(ctrl), ctrl)) {
+		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1)) {
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
Signature · 7 lines
2.51.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 3/18
#8 lore raw

Re: [PATCH 03/18] dbtr: Fix endian conversion in trigger install handler

Quoted 33 lines · Nicholas Piggin

An endian conversion was missed loading tdata1 from shm. Add the conversion and change the variable name from ctrl to tdata1 while here.

---
 lib/sbi/sbi_dbtr.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
lib/sbi/sbi_dbtr.c +4 -3
index 8f59e407..10ed6b3e 100644
@@ -589,7 +589,6 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	union sbi_dbtr_shmem_entry *entry;
 	struct sbi_dbtr_data_msg *recv;
 	struct sbi_dbtr_id_msg *xmit;
-	unsigned long ctrl;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
@@ -609,17 +608,19 @@ int sbi_dbtr_install_trig(unsigned long smode,
 
 	/* Check requested triggers configuration */
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
+		unsigned long tdata1;
+
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
-		ctrl = recv->tdata1;
+		tdata1 = lle_to_cpu(recv->tdata1);
 
-		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(ctrl))) {
+		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1))) {
 			*out = _idx;

lib/sbi/sbi_dbtr.c int sbi_dbtr_install_trig(unsigned long smode, · line 626

Quoted 10 lines
lib/sbi/sbi_dbtr.c +1 -1
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
 			return SBI_ERR_FAILED;
 		}
 
-		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(ctrl), ctrl)) {
+		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1)) {
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));

Looks good.

Thanks
Himanshu

Quoted 8 lines
Signature · 7 lines
2.51.0


-- 
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
#9 lore raw

PATCH 4/18 dbtr: Return correct error on install not supported

In reply to Nicholas Piggin

Return SBI_ERR_NOT_SUPPORTED if a trigger is valid but not supported.

---
 lib/sbi/sbi_dbtr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_dbtr.c +1 -1
index 10ed6b3e..65c3b2df 100644
@@ -617,7 +617,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
-			return SBI_ERR_FAILED;
+			return SBI_ERR_NOT_SUPPORTED;
 		}
 
 		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1)) {
Signature · 7 lines
2.51.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 4/18
#10 lore raw

Re: [PATCH 04/18] dbtr: Return correct error on install not supported

Quoted 13 lines · Nicholas Piggin

Return SBI_ERR_NOT_SUPPORTED if a trigger is valid but not supported.

---
 lib/sbi/sbi_dbtr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_dbtr.c +0 -0
index 10ed6b3e..65c3b2df 100644

lib/sbi/sbi_dbtr.c int sbi_dbtr_install_trig(unsigned long smode, · line 622

Quoted 7 lines
lib/sbi/sbi_dbtr.c +1 -1
@@ -617,7 +617,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
-			return SBI_ERR_FAILED;
+			return SBI_ERR_NOT_SUPPORTED;
 		}
 

Looks good.

Thanks
Himanshu

Quoted 9 lines
lib/sbi/sbi_dbtr.c +0 -0
 		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1)) {
Signature · 7 lines
2.51.0


-- 
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
#11 lore raw

PATCH 5/18 dbtr: Do not support chain bit

In reply to Nicholas Piggin

There is no chain bit validation in in SBI, so do not support it.

---
 lib/sbi/sbi_dbtr.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/sbi/sbi_dbtr.c +12 -2
index 65c3b2df..224f2350 100644
@@ -466,11 +466,21 @@ static void dbtr_trigger_clear(struct sbi_dbtr_trigger *trig)
 	csr_write(CSR_TDATA2, 0x0);
 }
 
-static int dbtr_trigger_supported(unsigned long type)
+static int dbtr_trigger_supported(unsigned long type, unsigned long tdata)
 {
 	switch (type) {
 	case RISCV_DBTR_TRIG_MCONTROL:
+		/*
+		 * SBI currently does not validate chain bit in install/update
+		 * so we do not support it.
+		 */
+		if (tdata & RV_DBTR_BIT_MASK(MC, CHAIN))
+			return 0;
+		return 1;
 	case RISCV_DBTR_TRIG_MCONTROL6:
+		if (tdata & RV_DBTR_BIT_MASK(MC6, CHAIN))
+			return 0;
+		return 1;
 	case RISCV_DBTR_TRIG_ICOUNT:
 		return 1;
 	default:
@@ -613,7 +623,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
 		tdata1 = lle_to_cpu(recv->tdata1);
 
-		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1))) {
+		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1), tdata1)) {
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
Signature · 7 lines
2.51.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 5/18
#12 lore raw

Re: [PATCH 05/18] dbtr: Do not support chain bit

Quoted 15 lines · Nicholas Piggin

There is no chain bit validation in in SBI, so do not support it.

---
 lib/sbi/sbi_dbtr.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/sbi/sbi_dbtr.c +0 -0
index 65c3b2df..224f2350 100644
@@ -466,11 +466,21 @@ static void dbtr_trigger_clear(struct sbi_dbtr_trigger *trig)
 	csr_write(CSR_TDATA2, 0x0);
 }
 

lib/sbi/sbi_dbtr.c static void dbtr_trigger_clear(struct sbi_dbtr_trigger *trig) · line 477

Quoted 10 lines
lib/sbi/sbi_dbtr.c +6 -1
-static int dbtr_trigger_supported(unsigned long type)
+static int dbtr_trigger_supported(unsigned long type, unsigned long tdata)
 {
 	switch (type) {
 	case RISCV_DBTR_TRIG_MCONTROL:
+		/*
+		 * SBI currently does not validate chain bit in install/update
+		 * so we do not support it.
+		 */
+		if (tdata & RV_DBTR_BIT_MASK(MC, CHAIN))

Just a minor suggestion. Since it may be supported in future, I would suggest that we print a warning message that a trigger with chain support was asked for.

lib/sbi/sbi_dbtr.c static void dbtr_trigger_clear(struct sbi_dbtr_trigger *trig) · line 481

Quoted 4 lines
lib/sbi/sbi_dbtr.c +3 -0
+			return 0;
+		return 1;
 	case RISCV_DBTR_TRIG_MCONTROL6:
+		if (tdata & RV_DBTR_BIT_MASK(MC6, CHAIN))

ditto

Otherwise looks good.

Thanks
Regards
Himanshu

Quoted 22 lines
lib/sbi/sbi_dbtr.c +3 -1
+			return 0;
+		return 1;
 	case RISCV_DBTR_TRIG_ICOUNT:
 		return 1;
 	default:
@@ -613,7 +623,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
 		tdata1 = lle_to_cpu(recv->tdata1);
 
-		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1))) {
+		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1), tdata1)) {
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
Signature · 7 lines
2.51.0


-- 
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
#13 lore raw

Re: [PATCH 05/18] dbtr: Do not support chain bit

Quoted 16 lines · Himanshu Chauhan
Quoted 15 lines · Nicholas Piggin

There is no chain bit validation in in SBI, so do not support it.

---
 lib/sbi/sbi_dbtr.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/sbi/sbi_dbtr.c +0 -0
index 65c3b2df..224f2350 100644
@@ -466,11 +466,21 @@ static void dbtr_trigger_clear(struct sbi_dbtr_trigger *trig)
 	csr_write(CSR_TDATA2, 0x0);
 }
 
Quoted 13 lines

lib/sbi/sbi_dbtr.c static void dbtr_trigger_clear(struct sbi_dbtr_trigger *trig) · line 477

Quoted 10 lines
lib/sbi/sbi_dbtr.c +6 -1
-static int dbtr_trigger_supported(unsigned long type)
+static int dbtr_trigger_supported(unsigned long type, unsigned long tdata)
 {
 	switch (type) {
 	case RISCV_DBTR_TRIG_MCONTROL:
+		/*
+		 * SBI currently does not validate chain bit in install/update
+		 * so we do not support it.
+		 */
+		if (tdata & RV_DBTR_BIT_MASK(MC, CHAIN))

Just a minor suggestion. Since it may be supported in future, I would suggest that we print a warning message that a trigger with chain support was asked for.

I'm not sure that is an appropriate way to report this.

There are already, in theory, certain configurations that may be supported by the underlying hardware, but are today rejected due to opensbi not having support for it (e.g., the other trigger types).

The idea of logging / warning is interesting, maybe under a "verbose" boot/build option? And the issue of missing hardware vs opensbi support for a feature is certainly something a user might be interested in. But I think it's for another patch series.

Thanks,
Nick

Quoted 42 lines

lib/sbi/sbi_dbtr.c static void dbtr_trigger_clear(struct sbi_dbtr_trigger *trig) · line 481

Quoted 5 lines
lib/sbi/sbi_dbtr.c +3 -0
+			return 0;
+		return 1;
 	case RISCV_DBTR_TRIG_MCONTROL6:
+		if (tdata & RV_DBTR_BIT_MASK(MC6, CHAIN))

ditto

Otherwise looks good.

Thanks
Regards
Himanshu

Quoted 22 lines
lib/sbi/sbi_dbtr.c +3 -1
+			return 0;
+		return 1;
 	case RISCV_DBTR_TRIG_ICOUNT:
 		return 1;
 	default:
@@ -613,7 +623,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
 		tdata1 = lle_to_cpu(recv->tdata1);
 
-		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1))) {
+		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1), tdata1)) {
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
Signature · 7 lines
2.51.0


-- 
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
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#14 lore raw

PATCH 6/18 dbtr: Improve trigger update error checking

In reply to Nicholas Piggin

Trigger updates should ensure all triggers can be upated without failure before making any changes. Updates that change the trigger type must also be disallowed according to SBI specification.

Change the style of shmem access and checking to match the trigger install code and perform all checks first. Add the missing check to prevent type change.

---
 lib/sbi/sbi_dbtr.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +26 -7
index 224f2350..d2845fec 100644
@@ -727,9 +727,9 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 int sbi_dbtr_update_trig(unsigned long smode,
 			 unsigned long trig_count, unsigned long *out)
 {
-	unsigned long trig_idx;
 	struct sbi_dbtr_trigger *trig;
 	union sbi_dbtr_shmem_entry *entry;
+	struct sbi_dbtr_data_msg *recv;
 	void *shmem_base = NULL;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
 
@@ -744,30 +744,49 @@ int sbi_dbtr_update_trig(unsigned long smode,
 		return SBI_ERR_NO_SHMEM;
 
 	shmem_base = hart_shmem_base(hs);
+	sbi_hart_protection_map_range((unsigned long)shmem_base,
+				      trig_count * sizeof(*entry));
 
+	/* Check requested triggers configuration */
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
-		sbi_hart_protection_map_range((unsigned long)entry, sizeof(*entry));
-		trig_idx = entry->id.idx;
+		unsigned long trig_idx, tdata1;
 
+		trig_idx = entry->id.idx;
 		if (trig_idx >= hs->total_trigs) {
-			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
 			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));
 			return SBI_ERR_INVALID_PARAM;
 		}
 
 		trig = INDEX_TO_TRIGGER(trig_idx);
-
 		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED))) {
-			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
 			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));
 			return SBI_ERR_FAILED;
 		}
 
+		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
+		tdata1 = lle_to_cpu(recv->tdata1);
+		if (TDATA1_GET_TYPE(tdata1) != TDATA1_GET_TYPE(trig->tdata1)) {
+			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));
+			return SBI_ERR_INVALID_PARAM;
+		}
+	}
+
+	/* Update triggers */
+	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
+		trig = INDEX_TO_TRIGGER(entry->id.idx);
 		dbtr_trigger_setup(trig, &entry->data);
-		sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
 		dbtr_trigger_enable(trig);
 	}
 
+	sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+					trig_count * sizeof(*entry));
+
 	return SBI_SUCCESS;
 }
 
Signature · 7 lines
2.51.0


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

Re: [PATCH 06/18] dbtr: Improve trigger update error checking

Quoted 29 lines · Nicholas Piggin

Trigger updates should ensure all triggers can be upated without failure before making any changes. Updates that change the trigger type must also be disallowed according to SBI specification.

Change the style of shmem access and checking to match the trigger install code and perform all checks first. Add the missing check to prevent type change.

---
 lib/sbi/sbi_dbtr.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +1 -1
index 224f2350..d2845fec 100644
@@ -727,9 +727,9 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 int sbi_dbtr_update_trig(unsigned long smode,
 			 unsigned long trig_count, unsigned long *out)
 {
-	unsigned long trig_idx;
 	struct sbi_dbtr_trigger *trig;
 	union sbi_dbtr_shmem_entry *entry;
+	struct sbi_dbtr_data_msg *recv;
 	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 748

Quoted 5 lines
lib/sbi/sbi_dbtr.c +2 -0
@@ -744,30 +744,49 @@ int sbi_dbtr_update_trig(unsigned long smode,
 		return SBI_ERR_NO_SHMEM;
 
 	shmem_base = hart_shmem_base(hs);
+	sbi_hart_protection_map_range((unsigned long)shmem_base,
+				      trig_count * sizeof(*entry));

Hi Nicholas,

I would suggest to verify the value of trig_count before making a large map entry. It would also help in the two loops below.

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

Quoted 13 lines
lib/sbi/sbi_dbtr.c +5 -3
+	/* Check requested triggers configuration */
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
-		sbi_hart_protection_map_range((unsigned long)entry, sizeof(*entry));
-		trig_idx = entry->id.idx;
+		unsigned long trig_idx, tdata1;
 
+		trig_idx = entry->id.idx;
 		if (trig_idx >= hs->total_trigs) {
-			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
 			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));

A forward goto to bailout in success/error condition would be better.

Thanks
Regards
Himanshu

Quoted 45 lines
lib/sbi/sbi_dbtr.c +18 -3
 			return SBI_ERR_INVALID_PARAM;
 		}
 
 		trig = INDEX_TO_TRIGGER(trig_idx);
-
 		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED))) {
-			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
 			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));
 			return SBI_ERR_FAILED;
 		}
 
+		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
+		tdata1 = lle_to_cpu(recv->tdata1);
+		if (TDATA1_GET_TYPE(tdata1) != TDATA1_GET_TYPE(trig->tdata1)) {
+			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));
+			return SBI_ERR_INVALID_PARAM;
+		}
+	}
+
+	/* Update triggers */
+	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
+		trig = INDEX_TO_TRIGGER(entry->id.idx);
 		dbtr_trigger_setup(trig, &entry->data);
-		sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
 		dbtr_trigger_enable(trig);
 	}
 
+	sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+					trig_count * sizeof(*entry));
+
 	return SBI_SUCCESS;
 }
 
Signature · 7 lines
2.51.0


-- 
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
#16 lore raw

Re: [PATCH 06/18] dbtr: Improve trigger update error checking

Quoted 47 lines · Himanshu Chauhan
Quoted 29 lines · Nicholas Piggin

Trigger updates should ensure all triggers can be upated without failure before making any changes. Updates that change the trigger type must also be disallowed according to SBI specification.

Change the style of shmem access and checking to match the trigger install code and perform all checks first. Add the missing check to prevent type change.

---
 lib/sbi/sbi_dbtr.c | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +1 -1
index 224f2350..d2845fec 100644
@@ -727,9 +727,9 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 int sbi_dbtr_update_trig(unsigned long smode,
 			 unsigned long trig_count, unsigned long *out)
 {
-	unsigned long trig_idx;
 	struct sbi_dbtr_trigger *trig;
 	union sbi_dbtr_shmem_entry *entry;
+	struct sbi_dbtr_data_msg *recv;
 	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 748

Quoted 5 lines
lib/sbi/sbi_dbtr.c +2 -0
@@ -744,30 +744,49 @@ int sbi_dbtr_update_trig(unsigned long smode,
 		return SBI_ERR_NO_SHMEM;
 
 	shmem_base = hart_shmem_base(hs);
+	sbi_hart_protection_map_range((unsigned long)shmem_base,
+				      trig_count * sizeof(*entry));

Hi Nicholas,

I would suggest to verify the value of trig_count before making a large map entry. It would also help in the two loops below.

Quoted 7 lines
lib/sbi/sbi_dbtr.c +2 -2
+	/* Check requested triggers configuration */
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
-		sbi_hart_protection_map_range((unsigned long)entry, sizeof(*entry));
-		trig_idx = entry->id.idx;
+		unsigned long trig_idx, tdata1;
 
Quoted 8 lines

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

Quoted 6 lines
lib/sbi/sbi_dbtr.c +3 -1
+		trig_idx = entry->id.idx;
 		if (trig_idx >= hs->total_trigs) {
-			sbi_hart_protection_unmap_range((unsigned long)entry, sizeof(*entry));
 			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));

A forward goto to bailout in success/error condition would be better.

Hey Himanshu,

Thanks for the reviews, I'll take a look at these suggestions and see how they look.

I should repost this series soon, I will do so after hearing from David about the tdata2/tdata3 issue.

Thanks,
Nick

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

PATCH 7/18 dbtr: Check for invalid and unsupported triggers in update

In reply to Nicholas Piggin

Update triggers does not check supported or valid, so it permits tdata1.m and tdata1.dmode bits being set.

Fix update to test that triggers are valid and supported.

---
 lib/sbi/sbi_dbtr.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
lib/sbi/sbi_dbtr.c +18 -2
index d2845fec..3925170f 100644
@@ -749,7 +749,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 
 	/* Check requested triggers configuration */
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
-		unsigned long trig_idx, tdata1;
+		unsigned long trig_idx, tdata1, type;
 
 		trig_idx = entry->id.idx;
 		if (trig_idx >= hs->total_trigs) {
@@ -769,7 +769,23 @@ int sbi_dbtr_update_trig(unsigned long smode,
 
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
 		tdata1 = lle_to_cpu(recv->tdata1);
-		if (TDATA1_GET_TYPE(tdata1) != TDATA1_GET_TYPE(trig->tdata1)) {
+		type = TDATA1_GET_TYPE(tdata1);
+
+		if (!dbtr_trigger_supported(type, tdata1)) {
+			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));
+			return SBI_ERR_NOT_SUPPORTED;
+		}
+
+		if (!dbtr_trigger_valid(type, tdata1)) {
+			*out = _idx;
+			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+							trig_count * sizeof(*entry));
+			return SBI_ERR_INVALID_PARAM;
+		}
+
+		if (type != TDATA1_GET_TYPE(trig->tdata1)) {
 			*out = _idx;
 			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 							trig_count * sizeof(*entry));
Signature · 7 lines
2.51.0


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

Re: [PATCH 07/18] dbtr: Check for invalid and unsupported triggers in update

Hi Nick,

I hit this independently while auditing DBTR, six months after you - sorry for not finding your series sooner.

It's still open on current master (5a175017). sbi_dbtr_update_trig() never calls dbtr_trigger_valid(), so S-mode can install a trigger that passes install's check and then set mcontrol6.M on it via TRIGGER_UPDATE. QEMU, single hart, no race:

install benign (M=0) : err=0, CSR_TDATA1 = 0x6000000000000014
update to M=1 : err=0, CSR_TDATA1 = 0x6000000000000054

Pointed at an address OpenSBI executes, the next ecall takes an M-mode breakpoint, which can't be redirected, and ends in sbi_hart_hang().

Are you planning to resend, either the series or 07/18 alone?

Also, your 01/18 and 18/18 are being re-done piecemeal without the correction between them. Pengpeng Hou's "validate complete shared memory range" adds the same install_trig check as your 01/18 - trig_count >= hs->total_trigs, SBI_ERR_BAD_RANGE - and has a Reviewed-by; my own v3 1/3 copied that form to avoid conflicting with it. Neither carries your 18/18 fix, and the effect is measurable: with total_trigs = 2, install(2) is rejected, so only total_trigs - 1 can be installed per call. Any news on riscv-sbi-doc#257?

Regards,
liutong

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

PATCH 8/18 dbtr: Improve error handling for trigger enable, disable, uninstall

In reply to Nicholas Piggin

When updating multiple triggers, error checking should be performed on all triggers before changing any. Check all triggers for mapping to HW in these handlers.

---
 lib/sbi/sbi_dbtr.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
lib/sbi/sbi_dbtr.c +24 -4
index 3925170f..a2c45061 100644
@@ -672,7 +672,7 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
 			    unsigned long trig_idx_mask)
 {
 	unsigned long trig_mask = trig_idx_mask << trig_idx_base;
-	unsigned long idx = trig_idx_base;
+	unsigned long idx;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs;
 
@@ -685,13 +685,17 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
 				trig_idx_base >= hs->total_trigs)
 		return SBI_ERR_INVALID_PARAM;
 
+	idx = trig_idx_base;
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
 		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
 			return SBI_ERR_INVALID_PARAM;
+	}
 
+	idx = trig_idx_base;
+	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
+		trig = INDEX_TO_TRIGGER(idx);
 		dbtr_trigger_clear(trig);
-
 		sbi_free_trigger(trig);
 	}
 
@@ -702,7 +706,7 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 			 unsigned long trig_idx_mask)
 {
 	unsigned long trig_mask = trig_idx_mask << trig_idx_base;
-	unsigned long idx = trig_idx_base;
+	unsigned long idx;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs;
 
@@ -715,6 +719,14 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 				trig_idx_base >= hs->total_trigs)
 		return SBI_ERR_INVALID_PARAM;
 
+	idx = trig_idx_base;
+	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
+		trig = INDEX_TO_TRIGGER(idx);
+		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
+			return SBI_ERR_INVALID_PARAM;
+	}
+
+	idx = trig_idx_base;
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
 		sbi_dprintf("%s: enable trigger %lu\n", __func__, idx);
@@ -810,7 +822,7 @@ int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
 			  unsigned long trig_idx_mask)
 {
 	unsigned long trig_mask = trig_idx_mask << trig_idx_base;
-	unsigned long idx = trig_idx_base;
+	unsigned long idx;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs;
 
@@ -823,6 +835,14 @@ int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
 				trig_idx_base >= hs->total_trigs)
 		return SBI_ERR_INVALID_PARAM;
 
+	idx = trig_idx_base;
+	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
+		trig = INDEX_TO_TRIGGER(idx);
+		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)))
+			return SBI_ERR_INVALID_PARAM;
+	}
+
+	idx = trig_idx_base;
 	for_each_set_bit_from(idx, &trig_mask, hs->total_trigs) {
 		trig = INDEX_TO_TRIGGER(idx);
 		dbtr_trigger_disable(trig);
Signature · 7 lines
2.51.0


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

PATCH 9/18 dbtr: Read triggers should not read HW trigger if not mapped

In reply to Nicholas Piggin

When no hardware trigger is mapped to a dbtr trig idx there is logically no hardware trigger to be read. The mapped status can be tested in the tstate word, so callers understand this.

---
 lib/sbi/sbi_dbtr.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +19 -7
index a2c45061..6efd5435 100644
@@ -577,14 +577,26 @@ int sbi_dbtr_read_trig(unsigned long smode,
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
 		xmit = &entry->data;
 		trig = INDEX_TO_TRIGGER((_idx + trig_idx_base));
-		csr_write(CSR_TSELECT, trig->index);
-		trig->tdata1 = csr_read(CSR_TDATA1);
-		trig->tdata2 = csr_read(CSR_TDATA2);
-		trig->tdata3 = csr_read(CSR_TDATA3);
+
 		xmit->tstate = cpu_to_lle(trig->state);
-		xmit->tdata1 = cpu_to_lle(trig->tdata1);
-		xmit->tdata2 = cpu_to_lle(trig->tdata2);
-		xmit->tdata3 = cpu_to_lle(trig->tdata3);
+		if (trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)) {
+			csr_write(CSR_TSELECT, trig->index);
+			trig->tdata1 = csr_read(CSR_TDATA1);
+			trig->tdata2 = csr_read(CSR_TDATA2);
+			trig->tdata3 = csr_read(CSR_TDATA3);
+			xmit->tdata1 = cpu_to_lle(trig->tdata1);
+			xmit->tdata2 = cpu_to_lle(trig->tdata2);
+			xmit->tdata3 = cpu_to_lle(trig->tdata3);
+		} else {
+			/*
+			 * If the trig idx does not have a HW trigger
+			 * assigned then just return zeroes in tdata
+			 * words.
+			 */
+			xmit->tdata1 = 0;
+			xmit->tdata2 = 0;
+			xmit->tdata3 = 0;
+		}
 	}
 	sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 					trig_count * sizeof(*entry));
Signature · 7 lines
2.51.0


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

PATCH 10/18 dbtr: Avoid crash in sbi_debug_read_triggers

In reply to Nicholas Piggin

Not all trigger types make tdata2 or tdata3 csrs available to read. This can result in crash in the SBI_EXT_DBTR_TRIGGER_READ ecall.

Fix this by using csr_read_allowed() to read those csrs. In the case of a trap nothing needs to be done since the caller will know those values are unavailable, based on the trigger type returned in tdata1.

---
 lib/sbi/sbi_dbtr.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_dbtr.c +4 -2
index 6efd5435..f72c76f2 100644
@@ -575,6 +575,8 @@ int sbi_dbtr_read_trig(unsigned long smode,
 	sbi_hart_protection_map_range((unsigned long)shmem_base,
 				      trig_count * sizeof(*entry));
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
+		struct sbi_trap_info trap = {0};
+
 		xmit = &entry->data;
 		trig = INDEX_TO_TRIGGER((_idx + trig_idx_base));
 
@@ -582,8 +584,8 @@ int sbi_dbtr_read_trig(unsigned long smode,
 		if (trig->state & RV_DBTR_BIT_MASK(TS, MAPPED)) {
 			csr_write(CSR_TSELECT, trig->index);
 			trig->tdata1 = csr_read(CSR_TDATA1);
-			trig->tdata2 = csr_read(CSR_TDATA2);
-			trig->tdata3 = csr_read(CSR_TDATA3);
+			trig->tdata2 = csr_read_allowed(CSR_TDATA2, &trap);
+			trig->tdata3 = csr_read_allowed(CSR_TDATA3, &trap);
 			xmit->tdata1 = cpu_to_lle(trig->tdata1);
 			xmit->tdata2 = cpu_to_lle(trig->tdata2);
 			xmit->tdata3 = cpu_to_lle(trig->tdata3);
Signature · 7 lines
2.51.0


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

PATCH 11/18 dbtr: Succeed operations with no triggers in mask

In reply to Nicholas Piggin

Trigger operations that use bitmasks should succeed if there are no triggers in the mask.

---
 lib/sbi/sbi_dbtr.c | 9 +++++++++
 1 file changed, 9 insertions(+)
lib/sbi/sbi_dbtr.c +9 -0
index f72c76f2..5735bdc6 100644
@@ -694,6 +694,9 @@ int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (!trig_idx_mask)
+		return SBI_SUCCESS;
+
 	if (trig_idx_base >= hs->total_trigs ||
 		find_last_bit(&trig_idx_mask, hs->total_trigs) +
 				trig_idx_base >= hs->total_trigs)
@@ -728,6 +731,9 @@ int sbi_dbtr_enable_trig(unsigned long trig_idx_base,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (!trig_idx_mask)
+		return SBI_SUCCESS;
+
 	if (trig_idx_base >= hs->total_trigs ||
 		find_last_bit(&trig_idx_mask, hs->total_trigs) +
 				trig_idx_base >= hs->total_trigs)
@@ -844,6 +850,9 @@ int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
+	if (!trig_idx_mask)
+		return SBI_SUCCESS;
+
 	if (trig_idx_base >= hs->total_trigs ||
 		find_last_bit(&trig_idx_mask, hs->total_trigs) +
 				trig_idx_base >= hs->total_trigs)
Signature · 7 lines
2.51.0


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

PATCH 12/18 dbtr: Move hardware trigger probing to a function

In reply to Nicholas Piggin

This makes the code a bit neater. HW trigger probing will be expanded with subsequent changes.

---
 lib/sbi/sbi_dbtr.c | 89 +++++++++++++++++++++++++---------------------
 1 file changed, 48 insertions(+), 41 deletions(-)
lib/sbi/sbi_dbtr.c +48 -41
index 5735bdc6..cfcd728f 100644
@@ -134,11 +134,57 @@ static inline void sbi_free_trigger(struct sbi_dbtr_trigger *trig)
 	hart_state->available_trigs++;
 }
 
-int sbi_dbtr_init(struct sbi_scratch *scratch, bool coldboot)
+static bool sbi_hw_trigger_probe(int i)
 {
 	struct sbi_trap_info trap = {0};
 	unsigned long tdata1;
 	unsigned long val;
+
+	csr_write_allowed(CSR_TSELECT, &trap, i);
+	if (trap.cause)
+		return false;
+
+	val = csr_read_allowed(CSR_TSELECT, &trap);
+	if (trap.cause)
+		return false;
+
+	/*
+	 * Read back tselect and check that it contains the
+	 * written value
+	 */
+	if (val != i)
+		return false;
+
+	val = csr_read_allowed(CSR_TINFO, &trap);
+	if (trap.cause) {
+		unsigned long type;
+
+		/*
+		 * If reading tinfo caused an exception, the
+		 * debugger must read tdata1 to discover the
+		 * type.
+		 */
+		tdata1 = csr_read_allowed(CSR_TDATA1, &trap);
+		if (trap.cause)
+			return false;
+
+		type = TDATA1_GET_TYPE(tdata1);
+		if (type == 0)
+			return false;
+
+		sbi_trigger_init(INDEX_TO_TRIGGER(i), BIT(type), i);
+	} else {
+		if (val == 1)
+			return false;
+
+		sbi_trigger_init(INDEX_TO_TRIGGER(i), val, i);
+	}
+
+	return true;
+}
+
+int sbi_dbtr_init(struct sbi_scratch *scratch, bool coldboot)
+{
 	int i;
 	struct sbi_dbtr_hart_triggers_state *hart_state = NULL;
 
@@ -168,47 +214,8 @@ int sbi_dbtr_init(struct sbi_scratch *scratch, bool coldboot)
 		goto _probed;
 
 	for (i = 0; i < RV_MAX_TRIGGERS; i++) {
-		csr_write_allowed(CSR_TSELECT, &trap, i);
-		if (trap.cause)
-			break;
-
-		val = csr_read_allowed(CSR_TSELECT, &trap);
-		if (trap.cause)
-			break;
-
-		/*
-		 * Read back tselect and check that it contains the
-		 * written value
-		 */
-		if (val != i)
-			break;
-
-		val = csr_read_allowed(CSR_TINFO, &trap);
-		if (trap.cause) {
-			/*
-			 * If reading tinfo caused an exception, the
-			 * debugger must read tdata1 to discover the
-			 * type.
-			 */
-			tdata1 = csr_read_allowed(CSR_TDATA1,
-						  &trap);
-			if (trap.cause)
-				break;
-
-			if (TDATA1_GET_TYPE(tdata1) == 0)
-				break;
-
-			sbi_trigger_init(INDEX_TO_TRIGGER(i),
-					 BIT(TDATA1_GET_TYPE(tdata1)),
-					 i);
+		if (sbi_hw_trigger_probe(i))
 			hart_state->total_trigs++;
-		} else {
-			if (val == 1)
-				break;
-
-			sbi_trigger_init(INDEX_TO_TRIGGER(i), val, i);
-			hart_state->total_trigs++;
-		}
 	}
 
 	hart_state->probed = 1;
Signature · 7 lines
2.51.0


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

PATCH 13/18 dbtr: Rework install and update error handling

In reply to Nicholas Piggin

Move the unmap error path to an error handling label rather than duplicate it several times.

---
 lib/sbi/sbi_dbtr.c | 54 +++++++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 24 deletions(-)
lib/sbi/sbi_dbtr.c +30 -24
index cfcd728f..3aea486e 100644
@@ -622,6 +622,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	struct sbi_dbtr_id_msg *xmit;
 	struct sbi_dbtr_trigger *trig;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
+	int err;
 
 	hs = dbtr_thishart_state_ptr();
 	if (!hs)
@@ -646,24 +647,21 @@ int sbi_dbtr_install_trig(unsigned long smode,
 
 		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1), tdata1)) {
 			*out = _idx;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-							trig_count * sizeof(*entry));
-			return SBI_ERR_NOT_SUPPORTED;
+			err = SBI_ERR_NOT_SUPPORTED;
+			goto out_unmap;
 		}
 
 		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1)) {
 			*out = _idx;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-							trig_count * sizeof(*entry));
-			return SBI_ERR_FAILED;
+			err = SBI_ERR_FAILED;
+			goto out_unmap;
 		}
 	}
 
 	if (hs->available_trigs < trig_count) {
 		*out = hs->available_trigs;
-		sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-					       trig_count * sizeof(*entry));
-		return SBI_ERR_FAILED;
+		err = SBI_ERR_FAILED;
+		goto out_unmap;
 	}
 
 	/* Install triggers */
@@ -687,6 +685,12 @@ int sbi_dbtr_install_trig(unsigned long smode,
 					trig_count * sizeof(*entry));
 
 	return SBI_SUCCESS;
+
+out_unmap:
+	sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+					trig_count * sizeof(*entry));
+
+	return err;
 }
 
 int sbi_dbtr_uninstall_trig(unsigned long trig_idx_base,
@@ -771,6 +775,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 	struct sbi_dbtr_data_msg *recv;
 	void *shmem_base = NULL;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
+	int err;
 
 	hs = dbtr_thishart_state_ptr();
 	if (!hs)
@@ -793,17 +798,15 @@ int sbi_dbtr_update_trig(unsigned long smode,
 		trig_idx = entry->id.idx;
 		if (trig_idx >= hs->total_trigs) {
 			*out = _idx;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-							trig_count * sizeof(*entry));
-			return SBI_ERR_INVALID_PARAM;
+			err = SBI_ERR_INVALID_PARAM;
+			goto out_unmap;
 		}
 
 		trig = INDEX_TO_TRIGGER(trig_idx);
 		if (!(trig->state & RV_DBTR_BIT_MASK(TS, MAPPED))) {
 			*out = _idx;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-							trig_count * sizeof(*entry));
-			return SBI_ERR_FAILED;
+			err = SBI_ERR_FAILED;
+			goto out_unmap;
 		}
 
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
@@ -812,23 +815,20 @@ int sbi_dbtr_update_trig(unsigned long smode,
 
 		if (!dbtr_trigger_supported(type, tdata1)) {
 			*out = _idx;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-							trig_count * sizeof(*entry));
-			return SBI_ERR_NOT_SUPPORTED;
+			err = SBI_ERR_NOT_SUPPORTED;
+			goto out_unmap;
 		}
 
 		if (!dbtr_trigger_valid(type, tdata1)) {
 			*out = _idx;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-							trig_count * sizeof(*entry));
-			return SBI_ERR_INVALID_PARAM;
+			err = SBI_ERR_INVALID_PARAM;
+			goto out_unmap;
 		}
 
 		if (type != TDATA1_GET_TYPE(trig->tdata1)) {
 			*out = _idx;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-							trig_count * sizeof(*entry));
-			return SBI_ERR_INVALID_PARAM;
+			err = SBI_ERR_INVALID_PARAM;
+			goto out_unmap;
 		}
 	}
 
@@ -843,6 +843,12 @@ int sbi_dbtr_update_trig(unsigned long smode,
 					trig_count * sizeof(*entry));
 
 	return SBI_SUCCESS;
+
+out_unmap:
+	sbi_hart_protection_unmap_range((unsigned long)shmem_base,
+					trig_count * sizeof(*entry));
+
+	return err;
 }
 
 int sbi_dbtr_disable_trig(unsigned long trig_idx_base,
Signature · 7 lines
2.51.0


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

PATCH 14/18 dbtr: Decouple dbtr trigger index from hardware trigger number

In reply to Nicholas Piggin

Some Sdtrig implementations have heterogeneous triggers, which require the dbtr trigger index be decoupled from the hardware trigger allocated to it.

The SBI interface supports this, prepare the implementation by creating a new sbi_hw_trigger data structure that is mapped by the sbi_dbtr_trigger with its HW_INDEX field.

The dbtr and hw triggers are still fixed to the same number.

---
 include/sbi/sbi_dbtr.h | 14 ++++++-
 lib/sbi/sbi_dbtr.c     | 87 ++++++++++++++++++++++++++++++------------
 2 files changed, 76 insertions(+), 25 deletions(-)
include/sbi/sbi_dbtr.h +13 -1
index b8b5c9ab..c0b9369e 100644
@@ -48,9 +48,14 @@ struct sbi_dbtr_shmem {
 	unsigned long phys_hi;
 };
 
+struct sbi_hw_trigger {
+	unsigned long index;
+	bool inuse; /* Allocated for a DBTR trigger */
+	unsigned long type_mask; /* Types supported by this trigger */
+};
+
 struct sbi_dbtr_trigger {
 	unsigned long index;
-	unsigned long type_mask;
 	unsigned long state;
 	unsigned long tdata1;
 	unsigned long tdata2;
@@ -69,6 +74,7 @@ struct sbi_dbtr_id_msg {
 };
 
 struct sbi_dbtr_hart_triggers_state {
+	struct sbi_hw_trigger hw_triggers[RV_MAX_TRIGGERS];
 	struct sbi_dbtr_trigger triggers[RV_MAX_TRIGGERS];
 	struct sbi_dbtr_shmem shmem;
 	u32 total_trigs;
@@ -89,6 +95,12 @@ struct sbi_dbtr_hart_triggers_state {
 			   & RV_DBTR_BIT_MASK(TS, HW_IDX));	\
 	}while (0);
 
+#define GET_TRIG_HW_INDEX(_state)				\
+	({							\
+		(_state & RV_DBTR_BIT_MASK(TS, HW_IDX))		\
+			    >> RV_DBTR_BIT(TS, HW_IDX);		\
+	})
+
 /** SBI shared mem messages layout */
 union sbi_dbtr_shmem_entry {
 	struct sbi_dbtr_data_msg data;
lib/sbi/sbi_dbtr.c +63 -24

212 lines not on this page · raw message

Signature · 7 lines
2.51.0


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

PATCH 15/18 dbtr: Move trigger feature support test into a function

In reply to Nicholas Piggin

With heterogeneous triggers this test becomes more complicated and is neeed in more places, so move it into a function.

---
 lib/sbi/sbi_dbtr.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/sbi/sbi_dbtr.c +9 -4
index 74b114b8..77162efe 100644
@@ -85,6 +85,14 @@ static inline void *hart_shmem_base(
 			hs->shmem.phys_hi, hs->shmem.phys_lo));
 }
 
+static bool sbi_hw_trigger_supports(struct sbi_hw_trigger *hw_trig,
+				unsigned long type, unsigned long tdata1)
+{
+	if (!(BIT(type) & hw_trig->type_mask))
+		return false;
+	return true;
+}
+
 static inline struct sbi_dbtr_trigger *sbi_alloc_trigger(void)
 {
 	int i;
@@ -555,7 +563,6 @@ int sbi_dbtr_num_trig(unsigned long data, unsigned long *out)
 	unsigned long type = TDATA1_GET_TYPE(data);
 	u32 hartid = current_hartid();
 	unsigned long total = 0;
-	struct sbi_hw_trigger *hw_trig;
 	int i;
 	struct sbi_dbtr_hart_triggers_state *hs;
 
@@ -569,9 +576,7 @@ int sbi_dbtr_num_trig(unsigned long data, unsigned long *out)
 	}
 
 	for (i = 0; i < hs->total_trigs; i++) {
-		hw_trig = INDEX_TO_HW_TRIGGER(i);
-
-		if (__test_bit(type, &hw_trig->type_mask))
+		if (sbi_hw_trigger_supports(INDEX_TO_HW_TRIGGER(i), type, data))
 			total++;
 	}
 
Signature · 7 lines
2.51.0


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

PATCH 16/18 dbtr: Heterogeneous trigger type support

In reply to Nicholas Piggin

The Tenstorrent Ascalon core has 8 triggers that support the mcontrol6 type and another that supports the icount type.

When installing SBI triggers, allocate HW triggers to them that satisfy their type requirement. A first pass is made over the requested SBI triggers which checks that all can be satisfied with available hardware triggers. If yes, then a second pass allocates each HW trigger and maps the corresponding SBI trigger to it.

---
 lib/sbi/sbi_dbtr.c | 104 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 74 insertions(+), 30 deletions(-)
lib/sbi/sbi_dbtr.c +74 -30
index 77162efe..f91a2b0d 100644
@@ -93,6 +93,31 @@ static bool sbi_hw_trigger_supports(struct sbi_hw_trigger *hw_trig,
 	return true;
 }
 
+static struct sbi_hw_trigger *sbi_alloc_hw_trigger(
+				unsigned long type, unsigned long tdata1)
+{
+	int i;
+	struct sbi_dbtr_hart_triggers_state *hart_state;
+
+	hart_state = dbtr_thishart_state_ptr();
+	if (!hart_state)
+		return NULL;
+
+	for (i = 0; i < hart_state->total_trigs; i++) {
+		struct sbi_hw_trigger *hw_trig = INDEX_TO_HW_TRIGGER(i);
+
+		if (hw_trig->inuse)
+			continue;
+		if (!sbi_hw_trigger_supports(hw_trig, type, tdata1))
+			continue;
+		hw_trig->inuse = true;
+
+		return hw_trig;
+	}
+
+	return NULL;
+}
+
 static inline struct sbi_dbtr_trigger *sbi_alloc_trigger(void)
 {
 	int i;
@@ -357,8 +382,6 @@ static void dbtr_trigger_setup(struct sbi_dbtr_trigger *trig,
 
 	SET_TRIG_HW_INDEX(trig->state, hw_trig->index);
 
-	hw_trig->inuse = true;
-
 	switch (TDATA1_GET_TYPE(tdata1)) {
 	case RISCV_DBTR_TRIG_MCONTROL:
 		if (__test_bit(RV_DBTR_BIT(MC, U), &tdata1))
@@ -654,8 +677,10 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	struct sbi_dbtr_data_msg *recv;
 	struct sbi_dbtr_id_msg *xmit;
 	struct sbi_dbtr_trigger *trig;
+	struct sbi_hw_trigger *hw_trig;
+	struct sbi_hw_trigger **hw_trigs;
 	struct sbi_dbtr_hart_triggers_state *hs = NULL;
-	int err;
+	int err, i;
 
 	hs = dbtr_thishart_state_ptr();
 	if (!hs)
@@ -671,64 +696,83 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	sbi_hart_protection_map_range((unsigned long)shmem_base,
 				      trig_count * sizeof(*entry));
 
-	/* Check requested triggers configuration */
+	/*
+	 * The first pass checks requested triggers configuration and allocates
+	 * HW triggers, putting them into an array indexed by the recv'ed
+	 * triggers. The second pass allocates DBTR triggers and maps them to
+	 * the HW triggers.
+	 */
+	hw_trigs = sbi_zalloc(sizeof(hw_trigs[0]) * trig_count);
+	i = 0;
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
-		unsigned long tdata1;
+		unsigned long tdata1, type;
 
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
 		tdata1 = lle_to_cpu(recv->tdata1);
+		type = TDATA1_GET_TYPE(tdata1);
 
-		if (!dbtr_trigger_supported(TDATA1_GET_TYPE(tdata1), tdata1)) {
+		if (!dbtr_trigger_supported(type, tdata1)) {
 			*out = _idx;
 			err = SBI_ERR_NOT_SUPPORTED;
-			goto out_unmap;
+			goto out_free_hw;
+		}
+
+		if (!dbtr_trigger_valid(type, tdata1)) {
+			*out = _idx;
+			err = SBI_ERR_INVALID_PARAM;
+			goto out_free_hw;
 		}
 
-		if (!dbtr_trigger_valid(TDATA1_GET_TYPE(tdata1), tdata1)) {
+		hw_trig = sbi_alloc_hw_trigger(type, tdata1);
+		if (!hw_trig) {
 			*out = _idx;
 			err = SBI_ERR_FAILED;
-			goto out_unmap;
+			goto out_free_hw;
 		}
-	}
 
-	if (hs->available_trigs < trig_count) {
-		*out = hs->available_trigs;
-		err = SBI_ERR_FAILED;
-		goto out_unmap;
+		hw_trigs[i] = hw_trig;
+		i++;
 	}
 
 	/* Install triggers */
+	i = 0;
 	for_each_trig_entry(shmem_base, trig_count, typeof(*entry), entry) {
-		struct sbi_hw_trigger *hw_trig;
-		/*
-		 * Since we have already checked if enough triggers are
-		 * available, trigger allocation must succeed.
-		 */
-		trig = sbi_alloc_trigger();
-
 		recv = (struct sbi_dbtr_data_msg *)(&entry->data);
 		xmit = (struct sbi_dbtr_id_msg *)(&entry->id);
 
-		hw_trig = INDEX_TO_HW_TRIGGER(trig->index); /* 1:1 trig / HW idx */
-		if (hw_trig->inuse) {
-			*out = hs->available_trigs;
-			sbi_hart_protection_unmap_range((unsigned long)shmem_base,
-					trig_count * sizeof(*entry));
-			sbi_printf("DBTR error: Hardware trigger already inuse\n");
-			return SBI_ERR_FAILED;
-		}
+		/*
+		 * HW triggers were all allocated in the previous pass. Now
+		 * allocate and map dbtr triggers to them.
+		 */
+		hw_trig = hw_trigs[i++];
+
+		/*
+		 * Since we have already checked if enough HW triggers are
+		 * available, dbtr trig allocation must succeed here.
+		 */
+		trig = sbi_alloc_trigger();
 
 		dbtr_trigger_setup(trig, hw_trig, recv);
 		dbtr_trigger_enable(trig);
 		xmit->idx = cpu_to_lle(trig->index);
 	}
 
+	sbi_free(hw_trigs);
 	sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 					trig_count * sizeof(*entry));
 
 	return SBI_SUCCESS;
 
-out_unmap:
+out_free_hw:
+	/* Free unused HW allocations */
+	for (i = 0; i < trig_count; i++) {
+		hw_trig = hw_trigs[i];
+		if (!hw_trig)
+			break;
+		hw_trig->inuse = false;
+	}
+
+	sbi_free(hw_trigs);
 	sbi_hart_protection_unmap_range((unsigned long)shmem_base,
 					trig_count * sizeof(*entry));
 
Signature · 7 lines
2.51.0


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

PATCH 17/18 dbtr: Heterogeneous access type matching for mcontrol triggers

In reply to Nicholas Piggin

The Tenstorrent Ascalon core has a set of mcontrol6 triggers which can match load/store, and a set which can match execute.

Detect access types supported in mc/mc6 triggers by writing 1 to load/store/access fields and reading back the result. Unsupported bits read back 0.

Account for this capability when allocating HW triggers.

---
 include/sbi/sbi_dbtr.h |  1 +
 lib/sbi/sbi_dbtr.c     | 77 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 76 insertions(+), 2 deletions(-)
include/sbi/sbi_dbtr.h +1 -0
index c0b9369e..16407f34 100644
@@ -52,6 +52,7 @@ struct sbi_hw_trigger {
 	unsigned long index;
 	bool inuse; /* Allocated for a DBTR trigger */
 	unsigned long type_mask; /* Types supported by this trigger */
+	unsigned long mcontrol_rwx_mask; /* rwx match bits supported */
 };
 
 struct sbi_dbtr_trigger {
lib/sbi/sbi_dbtr.c +75 -2

113 lines not on this page · raw message

Signature · 7 lines
2.51.0


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

PATCH 18/18 dbtr: Work around specification bug in range checks

In reply to Nicholas Piggin

The SBI spec appears to have an off-by-one / inequality error in range testing, reported here:

https://github.com/riscv-non-isa/riscv-sbi-doc/issues/257

This change updates range checking accordingly.

Note: may want to hold off this change until upstream spec confirms.

---
 lib/sbi/sbi_dbtr.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +4 -7
index f3186993..2edec1f5 100644
@@ -689,12 +689,9 @@ int sbi_dbtr_read_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
-	if (trig_count >= hs->total_trigs)
-		return SBI_ERR_BAD_RANGE;
-
 	if (trig_idx_base >= hs->total_trigs ||
-	    trig_idx_base + trig_count >= hs->total_trigs)
-		return SBI_ERR_INVALID_PARAM;
+	    trig_idx_base + trig_count > hs->total_trigs)
+		return SBI_ERR_BAD_RANGE;
 
 	if (sbi_dbtr_shmem_disabled(hs))
 		return SBI_ERR_NO_SHMEM;
@@ -752,7 +749,7 @@ int sbi_dbtr_install_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
-	if (trig_count >= hs->total_trigs)
+	if (trig_count > hs->total_trigs)
 		return SBI_ERR_BAD_RANGE;
 
 	if (sbi_dbtr_shmem_disabled(hs))
@@ -934,7 +931,7 @@ int sbi_dbtr_update_trig(unsigned long smode,
 	if (!hs)
 		return SBI_ERR_FAILED;
 
-	if (trig_count >= hs->total_trigs)
+	if (trig_count > hs->total_trigs)
 		return SBI_ERR_BAD_RANGE;
 
 	if (sbi_dbtr_shmem_disabled(hs))
Signature · 7 lines
2.51.0


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