Skip to contentSkip to conversation

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

PATCH New Clément Léger 4 participants 14 messages Jan 24, 2025 – Mar 24, 2025

lore
Share & tools
lore
#0 lore raw

v2 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.

---
v2:
- Use BIT_ULL instead of BIT to avoid shift overflow.
- Include another patch to optimize flags clearing.

Cl?ment L?ger (4):
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
lib: sbi: Clear lock only for local events

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

PATCH v2 1/4 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
Applied
#2 lore raw

PATCH v2 1/4 include: sbi: Add SBI_ERR_DENIED_LOCKED

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

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

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 40 lines
---
 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 -1
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
--

2.47.1

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

#3 lore raw

PATCH v2 2/4 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
#4 lore raw

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

Quoted 5 lines · On Fri, Jan 24, 2025 at 10:23?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

#5 lore raw

PATCH v2 3/4 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     | 10 +++++++---
 2 files changed, 9 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 +7 -3
index c46006d4..85f8a638 100644
@@ -287,12 +287,16 @@ 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_ULL(32) - 1))
+		return SBI_EINVAL;
+
 	tconf = get_feature_config(feature);
 	if (!tconf) {
 		if (fwft_is_defined_feature(feature))
@@ -323,7 +327,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 +352,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
#6 lore raw

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

? 2025-01-24?? 17:51 +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???? | 10 +++++++---
?2 files changed, 9 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..85f8a638 100644
@@ -287,12 +287,16 @@ 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 299

Quoted 4 lines
lib/sbi/sbi_fwft.c +4 -0
+	/* Feature are defined as 32 bits identifiers */
+	if (feature & ~(BIT_ULL(32) - 1))
+		return SBI_EINVAL;
+

We can specify the type of enumeration at the time of definition in the following way

enum sbi_fwft_feature_t : long {

In this way we don't need to change the declaration of the function, and we don't need the extra detection of the high 32 bits, because get_feature_config would return a null pointer because it couldn't find the

Regards,
Xiang W

Quoted 21 lines

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

lib/sbi/sbi_fwft.c +0 -0
@@ -323,7 +327,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 +352,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;

#7 lore raw

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

Quoted 52 lines · Xiang W

? 2025-01-24?? 17:51 +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???? | 10 +++++++---
?2 files changed, 9 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..85f8a638 100644
@@ -287,12 +287,16 @@ 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 299

Quoted 4 lines
lib/sbi/sbi_fwft.c +4 -0
+	/* Feature are defined as 32 bits identifiers */
+	if (feature & ~(BIT_ULL(32) - 1))
+		return SBI_EINVAL;
+

We can specify the type of enumeration at the time of definition in the following way

Quoted 1 line

enum sbi_fwft_feature_t : long {

TIL !:) After searching a bit, it seems like this is only available since C23. Is this something we allow in OpenSBI ?

Thanks,

Cl?ment

Quoted 30 lines

In this way we don't need to change the declaration of the function, and we don't need the extra detection of the high 32 bits, because get_feature_config would return a null pointer because it couldn't find the

Regards,
Xiang W

Quoted 21 lines

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

lib/sbi/sbi_fwft.c +0 -0
@@ -323,7 +327,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 +352,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;

#8 lore raw

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

? 2025-02-11?? 11:42 +0100?Cl?ment L?ger???

Quoted 55 lines · Xiang W
Quoted 52 lines

? 2025-01-24?? 17:51 +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???? | 10 +++++++---
?2 files changed, 9 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..85f8a638 100644
@@ -287,12 +287,16 @@ 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 299

Quoted 4 lines
lib/sbi/sbi_fwft.c +4 -0
+	/* Feature are defined as 32 bits identifiers */
+	if (feature & ~(BIT_ULL(32) - 1))
+		return SBI_EINVAL;
+

We can specify the type of enumeration at the time of definition in the following way

Quoted 8 lines
Quoted 1 line

enum sbi_fwft_feature_t : long {

TIL !:) After searching a bit, it seems like this is only available since C23. Is this something we allow in OpenSBI ?

Thanks,

Cl?ment

Add a value to the enumeration

enum sbi_fwft_feature_t {
	......
	SBI_FWFT_NOT_USED_ANYWHERE              = -1UL
};

The compiler will select the appropriate type to save the -1UL.

Regards,
Xiang W

Quoted 31 lines
Quoted 31 lines

In this way we don't need to change the declaration of the function, and we don't need the extra detection of the high 32 bits, because get_feature_config would return a null pointer because it couldn't find the

Regards,
Xiang W

Quoted 21 lines

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

lib/sbi/sbi_fwft.c +0 -0
@@ -323,7 +327,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 +352,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;

#9 lore raw

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

Quoted 65 lines · Xiang W

? 2025-02-11?? 11:42 +0100?Cl?ment L?ger???

Quoted 55 lines · Xiang W
Quoted 52 lines

? 2025-01-24?? 17:51 +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???? | 10 +++++++---
?2 files changed, 9 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..85f8a638 100644
@@ -287,12 +287,16 @@ 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 299

Quoted 4 lines
lib/sbi/sbi_fwft.c +4 -0
+	/* Feature are defined as 32 bits identifiers */
+	if (feature & ~(BIT_ULL(32) - 1))
+		return SBI_EINVAL;
+

We can specify the type of enumeration at the time of definition in the following way

Quoted 8 lines
Quoted 1 line

enum sbi_fwft_feature_t : long {

TIL !:) After searching a bit, it seems like this is only available since C23. Is this something we allow in OpenSBI ?

Thanks,

Cl?ment

Quoted 8 lines

Add a value to the enumeration

enum sbi_fwft_feature_t {
	......
	SBI_FWFT_NOT_USED_ANYWHERE              = -1UL
};

The compiler will select the appropriate type to save the -1UL.

I prefer to keep it as is, the enum only contains what is needed, the interface uses unsigned long and validates them to be passed to other functions.

Moreover doing so would be wrong regarding to the spec which states that features are 32 bits identifier. So modifying the feature enum storage to accommodate the internal interface is wrong.

Regards,

Cl?ment

Quoted 35 lines

Regards,
Xiang W

Quoted 31 lines
Quoted 31 lines

In this way we don't need to change the declaration of the function, and we don't need the extra detection of the high 32 bits, because get_feature_config would return a null pointer because it couldn't find the

Regards,
Xiang W

Quoted 21 lines

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

lib/sbi/sbi_fwft.c +0 -0
@@ -323,7 +327,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 +352,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;

#10 lore raw

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

In reply to Clément Léger
Quoted 19 lines · On Fri, Jan 24, 2025 at 10:23?PM Cl?ment L?ger <[email protected]> wrote:

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     | 10 +++++++---
 2 files changed, 9 insertions(+), 5 deletions(-)
include/sbi/sbi_fwft.h +0 -0
index 2148820e..a9caa863 100644

include/sbi/sbi_fwft.h line 19

Quoted 8 lines
include/sbi/sbi_fwft.h +2 -2
@@ -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);

The feature should be uint32_t as defined by the SBI spec.

Also, the uint32_t overflow check if required should be done in sbi_ecall_fwft_handler().

Regards,
Anup

Quoted 51 lines
include/sbi/sbi_fwft.h +0 -0
 int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot);
 
lib/sbi/sbi_fwft.c +7 -4
index c46006d4..85f8a638 100644
@@ -287,12 +287,16 @@ 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_ULL(32) - 1))
+               return SBI_EINVAL;
+
        tconf = get_feature_config(feature);
        if (!tconf) {
                if (fwft_is_defined_feature(feature))
@@ -323,7 +327,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 +352,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;
--

2.47.1

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

#11 lore raw

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

Quoted 31 lines · Anup Patel
Quoted 19 lines · On Fri, Jan 24, 2025 at 10:23?PM Cl?ment L?ger <[email protected]> wrote:

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     | 10 +++++++---
 2 files changed, 9 insertions(+), 5 deletions(-)
include/sbi/sbi_fwft.h +0 -0
index 2148820e..a9caa863 100644

include/sbi/sbi_fwft.h line 19

Quoted 8 lines
include/sbi/sbi_fwft.h +2 -2
@@ -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);

The feature should be uint32_t as defined by the SBI spec.

Quoted 2 lines

Also, the uint32_t overflow check if required should be done in sbi_ecall_fwft_handler().

Yes, I'll re-spin that patch with the SSE one as well.

Thanks,

Cl?ment

Quoted 55 lines

Regards,
Anup

Quoted 51 lines
include/sbi/sbi_fwft.h +0 -0
 int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot);
 
lib/sbi/sbi_fwft.c +7 -4
index c46006d4..85f8a638 100644
@@ -287,12 +287,16 @@ 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_ULL(32) - 1))
+               return SBI_EINVAL;
+
        tconf = get_feature_config(feature);
        if (!tconf) {
                if (fwft_is_defined_feature(feature))
@@ -323,7 +327,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 +352,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;
--

2.47.1

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

#12 lore raw

PATCH v2 4/4 lib: sbi: Clear lock only for local events

In reply to Clément Léger

Upon hart reset, only the local events state should be reset. While this implicitely worked since fwft_get_feature() return NULL for global events this is a bit fragile to do so. Additionally, the loop on feature list plus the call to fwft_get_feature was unneeded since we already have a direct access to the fwft hart state and thus can directly reset lock flags for each config. Lastly, we can only clear the lock when the fwft hart state was already allocated, in the other case, it is allocated with a zalloc which guarantees us that the feature flags are clear at boot.

---
 lib/sbi/sbi_fwft.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
lib/sbi/sbi_fwft.c +16 -10
index 85f8a638..d946e9db 100644
@@ -271,12 +271,17 @@ static int fwft_get_pmlen(struct fwft_config *conf, unsigned long *value)
 }
 #endif
 
+static bool fwft_feature_is_global(enum sbi_fwft_feature_t feature)
+{
+	return feature & SBI_FWFT_GLOBAL_FEATURE_BIT;
+}
+
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
 	int i;
 	struct fwft_hart_state *fhs = fwft_thishart_state_ptr();
 
-	if (feature & SBI_FWFT_GLOBAL_FEATURE_BIT)
+	if (fwft_feature_is_global(feature))
 		return NULL;
 
 	for (i = 0; i < fhs->config_count; i++){
@@ -315,16 +320,18 @@ static int fwft_get_feature(unsigned long feature,
 	return SBI_SUCCESS;
 }
 
-static void fwft_clear_config_lock(enum sbi_fwft_feature_t feature)
+static void fwft_clear_local_lock(struct fwft_hart_state *fhs)
 {
-	int ret;
+	int i;
 	struct fwft_config *conf;
 
-	ret = fwft_get_feature(feature, &conf);
-	if (ret)
-		return;
+	for (i = 0; i < fhs->config_count; i++) {
+		conf = &fhs->configs[i];
+		if (fwft_feature_is_global(conf->feature->id))
+			continue;
 
-	conf->flags &= ~SBI_FWFT_SET_FLAG_LOCK;
+		conf->flags &= ~SBI_FWFT_SET_FLAG_LOCK;
+	}
 }
 
 int sbi_fwft_set(unsigned long feature, unsigned long value,
@@ -428,10 +435,9 @@ int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot)
 			fhs->configs[i].feature = &features[i];
 
 		fwft_set_hart_state_ptr(scratch, fhs);
+	} else {
+		fwft_clear_local_lock(fhs);
 	}
 
-	for (i = 0; i < array_size(features); i++)
-		fwft_clear_config_lock(features[i].id);
-
 	return 0;
 }
Signature · 1 line
2.47.1
#13 lore raw

PATCH v2 4/4 lib: sbi: Clear lock only for local events

Quoted 78 lines · Cl?ment L?ger

Upon hart reset, only the local events state should be reset. While this implicitely worked since fwft_get_feature() return NULL for global events this is a bit fragile to do so. Additionally, the loop on feature list plus the call to fwft_get_feature was unneeded since we already have a direct access to the fwft hart state and thus can directly reset lock flags for each config. Lastly, we can only clear the lock when the fwft hart state was already allocated, in the other case, it is allocated with a zalloc which guarantees us that the feature flags are clear at boot.

---
 lib/sbi/sbi_fwft.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
lib/sbi/sbi_fwft.c +16 -10
index 85f8a638..d946e9db 100644
@@ -271,12 +271,17 @@ static int fwft_get_pmlen(struct fwft_config *conf, unsigned long *value)
 }
 #endif
 
+static bool fwft_feature_is_global(enum sbi_fwft_feature_t feature)
+{
+	return feature & SBI_FWFT_GLOBAL_FEATURE_BIT;
+}
+
 static struct fwft_config* get_feature_config(enum sbi_fwft_feature_t feature)
 {
 	int i;
 	struct fwft_hart_state *fhs = fwft_thishart_state_ptr();
 
-	if (feature & SBI_FWFT_GLOBAL_FEATURE_BIT)
+	if (fwft_feature_is_global(feature))
 		return NULL;
 
 	for (i = 0; i < fhs->config_count; i++){
@@ -315,16 +320,18 @@ static int fwft_get_feature(unsigned long feature,
 	return SBI_SUCCESS;
 }
 
-static void fwft_clear_config_lock(enum sbi_fwft_feature_t feature)
+static void fwft_clear_local_lock(struct fwft_hart_state *fhs)
 {
-	int ret;
+	int i;
 	struct fwft_config *conf;
 
-	ret = fwft_get_feature(feature, &conf);
-	if (ret)
-		return;
+	for (i = 0; i < fhs->config_count; i++) {
+		conf = &fhs->configs[i];
+		if (fwft_feature_is_global(conf->feature->id))
+			continue;
 
-	conf->flags &= ~SBI_FWFT_SET_FLAG_LOCK;
+		conf->flags &= ~SBI_FWFT_SET_FLAG_LOCK;
+	}
 }
 
 int sbi_fwft_set(unsigned long feature, unsigned long value,
@@ -428,10 +435,9 @@ int sbi_fwft_init(struct sbi_scratch *scratch, bool cold_boot)
 			fhs->configs[i].feature = &features[i];
 
 		fwft_set_hart_state_ptr(scratch, fhs);
+	} else {
+		fwft_clear_local_lock(fhs);
 	}
 
-	for (i = 0; i < array_size(features); i++)
-		fwft_clear_config_lock(features[i].id);
-
 	return 0;
 }
Signature · 1 line
2.47.1