Skip to contentSkip to conversation

riscv: cpu: improve multi-letter extension detection in supports_extension()

PATCH In review Conor Dooley 4 participants 5 messages Mar 4, 2024 – Mar 5, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: cpu: improve multi-letter extension detection in supports_extension()

From: Conor Dooley <[email protected]>

The first multi-letter extension after the single-letter extensions does not have to be preceded by an underscore, which could cause the parser to mistakenly find a single-letter extension after the start of the multi-letter portion of the string.
Three letters precede multi-letter extensions (s, x & z), none of which are valid single-letter extensions. The dt-binding also allows multi-letter extensions starting with h, but no such extension have been frozen or ratified, and the unprivileged spec no longer uses "h" as a prefix for multi-letter hypervisor extensions, having moved to "sh" instead. For that reason, modify the parser to stop at s, x & z to prevent this overrun, ignoring h.

---
The parser in U-Boot only supports single-letter extensions & the single-letter h has to be at the end of the single-letter section, so it would not be difficult to terminate parsing once a h is seen (you'd need to support the hypervisor extension to support additional hypervisor extensions after all) if in the future a multi-letter extension starting with h did come about. I've got no problem adding a special case for h, but I'm tempted to just remove the multi-letter h extensions from the binding, given there's actually not going to be any extensions ratified using that naming scheme.

CC Rick Chen <[email protected]>
CC Tom Rini <[email protected]>
CC Simon Glass <[email protected]>
CC Chanho Park <[email protected]>
CC Heinrich Schuchardt <[email protected]>
CC Bin Meng <[email protected]>
CC Conor Dooley <[email protected]>
---
 arch/riscv/cpu/cpu.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
arch/riscv/cpu/cpu.c +16 -6
index 8445c5823e..ecfefa1a02 100644
@@ -49,14 +49,24 @@ static inline bool supports_extension(char ext)
 	}
 	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
 		/*
-		 * skip the first 4 characters (rv32|rv64) and
-		 * check until underscore
+		 * skip the first 4 characters (rv32|rv64)
 		 */
 		for (i = 4; i < sizeof(desc); i++) {
-			if (desc[i] == '_' || desc[i] == '\0')
-				break;
-			if (desc[i] == ext)
-				return true;
+			switch (desc[i]) {
+			case 's':
+			case 'x':
+			case 'z':
+			case '_':
+			case '\0':
+				/*
+				 * Any of these characters mean the single
+				 * letter extensions have all been consumed.
+				 */
+				return false;
+			default:
+				if (desc[i] == ext)
+					return true;
+			}
 		}
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 65 lines · Conor Dooley

From: Conor Dooley <[email protected]>

The first multi-letter extension after the single-letter extensions does not have to be preceded by an underscore, which could cause the parser to mistakenly find a single-letter extension after the start of the multi-letter portion of the string.
Three letters precede multi-letter extensions (s, x & z), none of which are valid single-letter extensions. The dt-binding also allows multi-letter extensions starting with h, but no such extension have been frozen or ratified, and the unprivileged spec no longer uses "h" as a prefix for multi-letter hypervisor extensions, having moved to "sh" instead. For that reason, modify the parser to stop at s, x & z to prevent this overrun, ignoring h.

---
The parser in U-Boot only supports single-letter extensions & the single-letter h has to be at the end of the single-letter section, so it would not be difficult to terminate parsing once a h is seen (you'd need to support the hypervisor extension to support additional hypervisor extensions after all) if in the future a multi-letter extension starting with h did come about. I've got no problem adding a special case for h, but I'm tempted to just remove the multi-letter h extensions from the binding, given there's actually not going to be any extensions ratified using that naming scheme.

CC Rick Chen <[email protected]>
CC Tom Rini <[email protected]>
CC Simon Glass <[email protected]>
CC Chanho Park <[email protected]>
CC Heinrich Schuchardt <[email protected]>
CC Bin Meng <[email protected]>
CC Conor Dooley <[email protected]>
---
  arch/riscv/cpu/cpu.c | 22 ++++++++++++++++------
  1 file changed, 16 insertions(+), 6 deletions(-)
arch/riscv/cpu/cpu.c +9 -6
index 8445c5823e..ecfefa1a02 100644
@@ -49,14 +49,24 @@ static inline bool supports_extension(char ext)
  	}
  	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
  		/*
-		 * skip the first 4 characters (rv32|rv64) and
-		 * check until underscore
+		 * skip the first 4 characters (rv32|rv64)
  		 */
  		for (i = 4; i < sizeof(desc); i++) {
-			if (desc[i] == '_' || desc[i] == '\0')
-				break;
-			if (desc[i] == ext)
-				return true;
+			switch (desc[i]) {
+			case 's':
+			case 'x':
+			case 'z':
+			case '_':
+			case '\0':
+				/*
+				 * Any of these characters mean the single

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

Quoted 10 lines
arch/riscv/cpu/cpu.c +7 -0
+				 * letter extensions have all been consumed.
+				 */
+				return false;
+			default:
+				if (desc[i] == ext)
+					return true;
+			}
  		}
  	}
 

According to
https://github.com/riscv/riscv-isa-manual/blob/main/src/naming.adoc the ISA string is case insensitive. Why can we assume here that it is lower case?

Best regards

Heinrich

#3 lore raw
Quoted 9 lines · Conor Dooley
Quoted 6 lines · Heinrich Schuchardt
Quoted 1 line · Conor Dooley

From: Conor Dooley <[email protected]>

According to
https://github.com/riscv/riscv-isa-manual/blob/main/src/naming.adoc the ISA string is case insensitive. Why can we assume here that it is lower case?

The binding does not allow upper-case.

Ok. That is in Linux' Documentation/devicetree/bindings/riscv/extensions.yaml.

#4 lore raw
Quoted 9 lines · Heinrich Schuchardt
Quoted 8 lines · Conor Dooley
Quoted 6 lines · Heinrich Schuchardt
Quoted 1 line · Conor Dooley

From: Conor Dooley <[email protected]>

According to
https://github.com/riscv/riscv-isa-manual/blob/main/src/naming.adoc the ISA string is case insensitive. Why can we assume here that it is lower case?

Quoted 4 lines
Quoted 1 line

The binding does not allow upper-case.

Ok. That is in Linux' Documentation/devicetree/bindings/riscv/extensions.yaml.

Right. I should've linked to that, sorry.