Skip to contentSkip to conversation

riscv: Fix detecting FPU support in standard extension

PATCH Superseded Yu Chien Peter Lin 2 participants 3 messages Nov 4, 2022 – Nov 5, 2022 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Fix detecting FPU support in standard extension

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +11 -3
index 52ab02519f..dc949c1306 100644
@@ -36,6 +36,7 @@ static inline bool supports_extension(char ext)
 #ifdef CONFIG_CPU
 	struct udevice *dev;
 	char desc[32];
+	int i;
 
 	uclass_find_first_device(UCLASS_CPU, &dev);
 	if (!dev) {
@@ -43,9 +44,16 @@ static inline bool supports_extension(char ext)
 		return false;
 	}
 	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
-		/* skip the first 4 characters (rv32|rv64) */
-		if (strchr(desc + 4, ext))
-			return true;
+		/*
+		 * skip the first 4 characters (rv32|rv64) and
+		 * check until underscore
+		 */
+		for (i = 4; i < sizeof(desc); i++) {
+			if (!(desc[i] - '_'))
+				break;
+			if (desc[i] == ext)
+				return true;
+		}
 	}
 
 	return false;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 26 lines · Yu Chien Peter Lin

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +1 -0
index 52ab02519f..dc949c1306 100644
@@ -36,6 +36,7 @@ static inline bool supports_extension(char ext)
 #ifdef CONFIG_CPU
 	struct udevice *dev;
 	char desc[32];
+	int i;
 
 	uclass_find_first_device(UCLASS_CPU, &dev);
 	if (!dev) {
@@ -43,9 +44,16 @@ static inline bool supports_extension(char ext)
 		return false;
 	}

arch/riscv/cpu/cpu.c static inline bool supports_extension(char ext) · line 52

Quoted 10 lines
arch/riscv/cpu/cpu.c +6 -3
 	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
-		/* skip the first 4 characters (rv32|rv64) */
-		if (strchr(desc + 4, ext))
-			return true;
+		/*
+		 * skip the first 4 characters (rv32|rv64) and
+		 * check until underscore
+		 */
+		for (i = 4; i < sizeof(desc); i++) {
+			if (!(desc[i] - '_'))

Why not use "if (desc[i] == '_')"? Also, you need to stop at the null terminator.

Regards,
Samuel

Quoted 7 lines
arch/riscv/cpu/cpu.c +4 -0
+				break;
+			if (desc[i] == ext)
+				return true;
+		}
 	}
 
 	return false;
#2 lore raw
Quoted 41 lines · Samuel Holland
Quoted 26 lines · Yu Chien Peter Lin

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +1 -0
index 52ab02519f..dc949c1306 100644
@@ -36,6 +36,7 @@ static inline bool supports_extension(char ext)
 #ifdef CONFIG_CPU
 	struct udevice *dev;
 	char desc[32];
+	int i;
 
 	uclass_find_first_device(UCLASS_CPU, &dev);
 	if (!dev) {
@@ -43,9 +44,16 @@ static inline bool supports_extension(char ext)
 		return false;
 	}

arch/riscv/cpu/cpu.c static inline bool supports_extension(char ext) · line 52

Quoted 10 lines
arch/riscv/cpu/cpu.c +6 -3
 	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
-		/* skip the first 4 characters (rv32|rv64) */
-		if (strchr(desc + 4, ext))
-			return true;
+		/*
+		 * skip the first 4 characters (rv32|rv64) and
+		 * check until underscore
+		 */
+		for (i = 4; i < sizeof(desc); i++) {
+			if (!(desc[i] - '_'))

Why not use "if (desc[i] == '_')"? Also, you need to stop at the null terminator.

Quoted 2 lines

Regards,
Samuel

Hi Samuel,

Oops, I'll fix this.
Thanks for your review!

Best regards,
Peter Lin

Quoted 9 lines
Quoted 8 lines
arch/riscv/cpu/cpu.c +4 -0
+				break;
+			if (desc[i] == ext)
+				return true;
+		}
 	}
 
 	return false;