Skip to contentSkip to conversation

lib: sbi: fwft: Fix return error values according to the spec

PATCH Superseded Clément Léger 3 participants 7 messages Jan 23, 2025 – Feb 19, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover lib: sbi: fwft: Fix return error values according to the spec

The fwft spec mandates that an invalid feature ID return SBI_ERR_INVALID_PARAM and that setting a locked value should return SBI_ERR_DENIED_LOCKED. Add support for both cases.

Cl?ment L?ger (3):
include: sbi: Add SBI_ERR_DENIED_LOCKED
lib: sbi: fwft: Return SBI_ERR_DENIED_LOCKED when setting a locked
feature
lib: sbi: fwft: Fix error return value for feature > 32 bits

 include/sbi/sbi_ecall_interface.h |  3 ++-
 include/sbi/sbi_error.h           |  1 +
 include/sbi/sbi_fwft.h            |  4 ++--
 lib/sbi/sbi_fwft.c                | 13 +++++++++----
 4 files changed, 14 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/3 include: sbi: Add SBI_ERR_DENIED_LOCKED

Add SBI_ERR_DENIED_LOCKED and set it as the SBI_LAST_ERR which was wrongly set to SBI_ERR_BAD_RANGE.

---
 include/sbi/sbi_ecall_interface.h | 3 ++-
 include/sbi/sbi_error.h           | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +2 -1
index 0b6b5714..cac347a1 100644
@@ -455,8 +455,9 @@ enum sbi_sse_state {
 #define SBI_ERR_BAD_RANGE			-11
 #define SBI_ERR_TIMEOUT				-12
 #define SBI_ERR_IO				-13
+#define SBI_ERR_DENIED_LOCKED			-14
 
-#define SBI_LAST_ERR				SBI_ERR_BAD_RANGE
+#define SBI_LAST_ERR				SBI_ERR_DENIED_LOCKED
 
 /* clang-format on */
 
include/sbi/sbi_error.h +1 -0
index b40b3319..febd596e 100644
@@ -29,6 +29,7 @@
 #define SBI_ETIMEOUT		SBI_ERR_TIMEOUT
 #define SBI_ETIMEDOUT		SBI_ERR_TIMEOUT
 #define SBI_EIO			SBI_ERR_IO
+#define SBI_EDENIED_LOCKED	SBI_ERR_DENIED_LOCKED
 
 #define SBI_ENODEV		-1000
 #define SBI_ENOSYS		-1001
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/3 lib: sbi: fwft: Return SBI_ERR_DENIED_LOCKED when setting a locked feature

In reply to Clément Léger

Latest modifications to the spec mandates that a set on a lock feature returns SBI_ERR_DENIED_LOCKED.

---
 lib/sbi/sbi_fwft.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +1 -1
index 0cb165b4..c46006d4 100644
@@ -337,7 +337,7 @@ int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
 		return SBI_EINVAL;
 
 	if (conf->flags & SBI_FWFT_SET_FLAG_LOCK)
-		return SBI_EDENIED;
+		return SBI_EDENIED_LOCKED;
 
 	ret = conf->feature->set(conf, value);
 	if (ret)
Signature · 1 line
2.47.1
Applied
#3 lore raw

PATCH 2/3 lib: sbi: fwft: Return SBI_ERR_DENIED_LOCKED when setting a locked feature

Quoted 5 lines · On Thu, Jan 23, 2025 at 9:36?PM Cl?ment L?ger <[email protected]> wrote:

Latest modifications to the spec mandates that a set on a lock feature returns SBI_ERR_DENIED_LOCKED.

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 25 lines
---
 lib/sbi/sbi_fwft.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_fwft.c +1 -2
index 0cb165b4..c46006d4 100644
@@ -337,7 +337,7 @@ int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
                return SBI_EINVAL;
 
        if (conf->flags & SBI_FWFT_SET_FLAG_LOCK)
-               return SBI_EDENIED;
+               return SBI_EDENIED_LOCKED;
 
        ret = conf->feature->set(conf, value);
        if (ret)
--

2.47.1

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

#4 lore raw

PATCH 3/3 lib: sbi: fwft: Fix error return value for feature > 32 bits

In reply to Clément Léger

Based on the spec, invalid feature should return SBI_ERR_INVALID_PARAM. This was not the case with the current implementation since the upper 32 bits were not considered when checking the feature thus returning SBI_ERR_DENIED. Fix that by passing the raw unsigned long rather than the enum to be sure we are passing the full 64 bits and then check for upper bits to be cleared.

---
 include/sbi/sbi_fwft.h |  4 ++--
 lib/sbi/sbi_fwft.c     | 11 ++++++++---
 2 files changed, 10 insertions(+), 5 deletions(-)
include/sbi/sbi_fwft.h +2 -2
index 2148820e..a9caa863 100644
@@ -14,9 +14,9 @@
 
 struct sbi_scratch;
 
-int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
+int sbi_fwft_set(unsigned long feature, unsigned long value,
 		 unsigned long flags);
-int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val);
+int sbi_fwft_get(unsigned long feature, unsigned long *out_val);
 
 int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot);
 
lib/sbi/sbi_fwft.c +8 -3
index c46006d4..7bf86d6d 100644
@@ -287,12 +287,17 @@ static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 	return NULL;
 }
 
-static int fwft_get_feature(enum sbi_fwft_feature_t feature,
+static int fwft_get_feature(unsigned long feature,
 			    struct fwft_config **conf)
 {
 	int ret;
 	struct fwft_config *tconf;
 
+	/* Feature are defined as 32 bits identifiers */
+	if (feature & ~(BIT(32) - 1)) {
+		return SBI_EINVAL;
+	}
+
 	tconf = get_feature_config(feature);
 	if (!tconf) {
 		if (fwft_is_defined_feature(feature))
@@ -323,7 +328,7 @@ static void fwft_clear_config_lock(enum sbi_fwft_feature_t feature)
 	conf->flags &= ~SBI_FWFT_SET_FLAG_LOCK;
 }
 
-int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
+int sbi_fwft_set(unsigned long feature, unsigned long value,
 		 unsigned long flags)
 {
 	int ret;
@@ -348,7 +353,7 @@ int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
 	return SBI_OK;
 }
 
-int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val)
+int sbi_fwft_get(unsigned long feature, unsigned long *out_val)
 {
 	int ret;
 	struct fwft_config *conf;
Signature · 1 line
2.47.1
#5 lore raw

PATCH 3/3 lib: sbi: fwft: Fix error return value for feature > 32 bits

? 2025-01-23?? 17:05 +0100?Cl?ment L?ger???

Quoted 44 lines

Based on the spec, invalid feature should return SBI_ERR_INVALID_PARAM. This was not the case with the current implementation since the upper 32 bits were not considered when checking the feature thus returning SBI_ERR_DENIED. Fix that by passing the raw unsigned long rather than the enum to be sure we are passing the full 64 bits and then check for upper bits to be cleared.

---
?include/sbi/sbi_fwft.h |? 4 ++-- ?lib/sbi/sbi_fwft.c???? | 11 ++++++++--- ?2 files changed, 10 insertions(+), 5 deletions(-)

include/sbi/sbi_fwft.h +0 -0
index 2148820e..a9caa863 100644
@@ -14,9 +14,9 @@

?
?struct sbi_scratch;
?

include/sbi/sbi_fwft.h +1 -1
-int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
+int sbi_fwft_set(unsigned long feature, unsigned long value,

? unsigned long flags);

include/sbi/sbi_fwft.h +1 -1
-int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val);
+int sbi_fwft_get(unsigned long feature, unsigned long *out_val);

?
?int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot); ?

lib/sbi/sbi_fwft.c +0 -0
index c46006d4..7bf86d6d 100644
@@ -287,12 +287,17 @@ static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 ?	return NULL;
 ?}

?

lib/sbi/sbi_fwft.c +1 -1
-static int fwft_get_feature(enum sbi_fwft_feature_t feature,
+static int fwft_get_feature(unsigned long feature,

? ??? struct fwft_config **conf)
?{
? int ret;
? struct fwft_config *tconf;
?

lib/sbi/sbi_fwft.c static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature) · line 297

Quoted 2 lines
lib/sbi/sbi_fwft.c +2 -0
+	/* Feature are defined as 32 bits identifiers */
+	if (feature & ~(BIT(32) - 1)) {

The macro expansion of BIT(32) is 1UL<<32. In RV32, unsigned long is 32 bits,?so integer overflow will occur. It is recommended to replace BIT with BIT_ULL.

Regards,
Xiang W

Quoted 28 lines
lib/sbi/sbi_fwft.c +3 -0
+		return SBI_EINVAL;
+	}
+

? tconf = get_feature_config(feature);
? if (!tconf) {
? if (fwft_is_defined_feature(feature))

lib/sbi/sbi_fwft.c +0 -0
@@ -323,7 +328,7 @@ static void fwft_clear_config_lock(enum sbi_fwft_feature_t feature)
 ?	conf->flags &= ~SBI_FWFT_SET_FLAG_LOCK;
 ?}

?

lib/sbi/sbi_fwft.c +1 -1
-int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
+int sbi_fwft_set(unsigned long feature, unsigned long value,

? unsigned long flags)
?{
? int ret;

lib/sbi/sbi_fwft.c +0 -0
@@ -348,7 +353,7 @@ int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
 ?	return SBI_OK;
 ?}

?

lib/sbi/sbi_fwft.c +1 -1
-int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val)
+int sbi_fwft_get(unsigned long feature, unsigned long *out_val)

?{
? int ret;
? struct fwft_config *conf;

Signature · 1 line
2.47.1
#6 lore raw

PATCH 3/3 lib: sbi: fwft: Fix error return value for feature > 32 bits

Quoted 41 lines · Xiang W

? 2025-01-23?? 17:05 +0100?Cl?ment L?ger???

Quoted 40 lines

Based on the spec, invalid feature should return SBI_ERR_INVALID_PARAM. This was not the case with the current implementation since the upper 32 bits were not considered when checking the feature thus returning SBI_ERR_DENIED. Fix that by passing the raw unsigned long rather than the enum to be sure we are passing the full 64 bits and then check for upper bits to be cleared.

---
?include/sbi/sbi_fwft.h |? 4 ++-- ?lib/sbi/sbi_fwft.c???? | 11 ++++++++--- ?2 files changed, 10 insertions(+), 5 deletions(-)

include/sbi/sbi_fwft.h +0 -0
index 2148820e..a9caa863 100644
@@ -14,9 +14,9 @@

?
?struct sbi_scratch;
?

include/sbi/sbi_fwft.h +1 -1
-int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
+int sbi_fwft_set(unsigned long feature, unsigned long value,

? unsigned long flags);

include/sbi/sbi_fwft.h +1 -1
-int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val);
+int sbi_fwft_get(unsigned long feature, unsigned long *out_val);

?
?int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot); ?

lib/sbi/sbi_fwft.c +0 -0
index c46006d4..7bf86d6d 100644
@@ -287,12 +287,17 @@ static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 ?	return NULL;
 ?}

?

lib/sbi/sbi_fwft.c +1 -1
-static int fwft_get_feature(enum sbi_fwft_feature_t feature,
+static int fwft_get_feature(unsigned long feature,

? ??? struct fwft_config **conf)

Quoted 8 lines

lib/sbi/sbi_fwft.c static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature) · line 297

Quoted 6 lines

?{
? int ret;
? struct fwft_config *tconf;
?

lib/sbi/sbi_fwft.c +2 -0
+	/* Feature are defined as 32 bits identifiers */
+	if (feature & ~(BIT(32) - 1)) {

The macro expansion of BIT(32) is 1UL<<32. In RV32, unsigned long is 32 bits,?so integer overflow will occur. It is recommended to replace BIT with BIT_ULL.

Oh yes nice catch. That is UB indeed. I'll fix that.

Quoted 32 lines

Regards,
Xiang W

Quoted 28 lines
lib/sbi/sbi_fwft.c +3 -0
+		return SBI_EINVAL;
+	}
+

? tconf = get_feature_config(feature);
? if (!tconf) {
? if (fwft_is_defined_feature(feature))

lib/sbi/sbi_fwft.c +0 -0
@@ -323,7 +328,7 @@ static void fwft_clear_config_lock(enum sbi_fwft_feature_t feature)
 ?	conf->flags &= ~SBI_FWFT_SET_FLAG_LOCK;
 ?}

?

lib/sbi/sbi_fwft.c +1 -1
-int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
+int sbi_fwft_set(unsigned long feature, unsigned long value,

? unsigned long flags)
?{
? int ret;

lib/sbi/sbi_fwft.c +0 -0
@@ -348,7 +353,7 @@ int sbi_fwft_set(enum sbi_fwft_feature_t feature, unsigned long value,
 ?	return SBI_OK;
 ?}

?

lib/sbi/sbi_fwft.c +1 -1
-int sbi_fwft_get(enum sbi_fwft_feature_t feature, unsigned long *out_val)
+int sbi_fwft_get(unsigned long feature, unsigned long *out_val)

?{
? int ret;
? struct fwft_config *conf;

Signature · 1 line
2.47.1