Skip to contentSkip to conversation

sparc64: Read qemu,hwcap-list on sun4u

PATCH In review Stian Halseth 2 participants 5 messages Nov 18 SPARC + UltraSPARC (sparc/sparc64)

lore
Share & tools
lore
#0 lore raw

v2 cover sparc64: Read qemu,hwcap-list on sun4u

QEMU's default sparc64 machine is sun4u. With -cpu ...,+crypto it emulates the SPARC crypto opcodes and passes the hwcap-list to OpenBIOS. OpenBIOS puts the list on the cpu node as qemu,hwcap-list. Patch 2 reads it there. The crypto KUnit tests then run the sparc64 code under QEMU.

Patch 1 is new. Sashiko found that the hwcap-list parser reads past an entry that has no NUL. The machine description parser has the same bug, so patch 1 fixes it before patch 2 moves the code.

On sun4u, the ecb-, cbc- and ctr-aes-sparc64 glue loses its round keys in skcipher_walk_done(). Under QEMU with +crypto, their self-tests fail. No real sun4u CPU has the crypto opcodes. Eric's series moves these modes into lib/crypto and fixes it:
https://lore.kernel.org/linux-crypto/[email protected]/
It is planned for 7.5 via libcrypto-next.

The QEMU side is in master for 11.2, commit a1eb9a1720 ("hw/sparc64:
Pass hwcap-list to OpenBIOS via fw_cfg"). The OpenBIOS side is on the OpenBIOS list:
https://mail.coreboot.org/archives/list/[email protected]/thread/3XVXVLHWT2T63IYWR67WPOHFSJGLP3NP/
Until QEMU ships an OpenBIOS with it, nvramrc can set the property:

NV='" /SUNW,UltraSPARC-IIi" find-device " vis" encode-string " vis2" encode-string encode+ " vis3" encode-string encode+ " aes" encode-string encode+ " sha1" encode-string encode+ " sha256" encode-string encode+ " sha512" encode-string encode+ " crc32c" encode-string encode+ " qemu,hwcap-list" property device-end'

./tools/testing/kunit/kunit.py run --arch=sparc64 \
	--kunitconfig=lib/crypto/.kunitconfig \
	--kunitconfig=lib/crc/.kunitconfig \
	--qemu_args='-cpu TI-UltraSparc-IIi,+vis3,+crypto' \
	--qemu_args='-prom-env use-nvramrc?=true' \
	--qemu_args="-prom-env 'nvramrc=$NV'"

Changes in v2:
- New patch 1: stop the parser at an entry without a NUL (Sashiko).
- Patch 2: Eric's Tested-by. No other change.
- Dropped the lib/crypto question from the notes. Eric's series answers it.

v1: https://lore.kernel.org/all/[email protected]/

Stian Halseth (2):
sparc64: Stop the hwcap-list parser at an unterminated entry
sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u

 arch/sparc/kernel/setup_64.c | 68 ++++++++++++++++++++++++++----------
 1 file changed, 50 insertions(+), 18 deletions(-)

base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 sparc64: Stop the hwcap-list parser at an unterminated entry

mdesc_cpu_hwcap_list() walks the hwcap-list strings with strlen() and strcmp(). If the last entry has no NUL within the property, both read past its end. len then goes negative, and while (len) does not stop.

Measure each entry with strnlen() first, and stop at one that does not fit, as of_property_read_string_helper() does.

---
 arch/sparc/kernel/setup_64.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/sparc/kernel/setup_64.c +4 -1
index 63615f5c99b47..96de9fe84cd44 100644
@@ -489,6 +489,10 @@ static unsigned long __init mdesc_cpu_hwcap_list(void)
 	while (len) {
 		int i, plen;
 
+		plen = strnlen(prop, len) + 1;
+		if (plen > len)
+			break;
+
 		for (i = 0; i < ARRAY_SIZE(hwcaps); i++) {
 			unsigned long bit = 1UL << i;
 
@@ -502,7 +506,6 @@ static unsigned long __init mdesc_cpu_hwcap_list(void)
 				caps |= HWCAP_SPARC_CRYPTO;
 		}
 
-		plen = strlen(prop) + 1;
 		prop += plen;
 		len -= plen;
 	}
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/2] sparc64: Stop the hwcap-list parser at an unterminated entry

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] sparc64: missing Fixes trailer for mdesc_cpu_hwcap_list() bug fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] sparc64: missing Fixes trailer for mdesc_cpu_hwcap_list() bug fix
--

commit d3a59092fe2c64c80fb8dba94ba6f29bf1d288f4
Author: Stian Halseth <[email protected]>

Quoted 10 lines

sparc64: Stop the hwcap-list parser at an unterminated entry

mdesc_cpu_hwcap_list() walks the hwcap-list strings with strlen() and strcmp(). If the last entry has no NUL within the property, both read past its end. len then goes negative, and while (len) does not stop.

Measure each entry with strnlen() first, and stop at one that does not fit, as of_property_read_string_helper() does.

[Severity: Low]
This fixes a bug in mdesc_cpu_hwcap_list(). Should this include:

Fixes ac85fe8b2124 ("sparc: Sanitize cpu feature detection and reporting.")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 1/2] sparc64: Stop the hwcap-list parser at an unterminated entry

Hi Andreas,

Quoted 7 lines · On Wed, 2026-11-18 at 12:07 +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential

[Severity: Low]
This fixes a bug in mdesc_cpu_hwcap_list(). Should this include:

Fixes ac85fe8b2124 ("sparc: Sanitize cpu feature detection and reporting.")

Yes, the loop came in with that commit.

Fixes ac85fe8b2124 ("sparc: Sanitize cpu feature detection and reporting.")

Could you add it when applying? If there is a v3, I'll fold it in.

/Stian

#4 lore raw

PATCH v2 2/2 sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u

In reply to Stian Halseth

The SPARC crypto opcodes are used only when hwcap-list names them. Only the sun4v machine description carries a hwcap-list. QEMU's default sparc64 machine is sun4u and has no machine description. The kernel then never uses QEMU's crypto instructions, and the crypto KUnit tests only exercise the generic code.

For CPUs with the crypto feature, QEMU passes a hwcap-list to the firmware. OpenBIOS puts it on the cpu node as qemu,hwcap-list. Real sun4u firmware has no such property.

Factor out the hwcap-list parser. When there is no machine description, read the list from that property.

---
 arch/sparc/kernel/setup_64.c | 63 ++++++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 17 deletions(-)
arch/sparc/kernel/setup_64.c +46 -17
index 96de9fe84cd44..dde144adec82a 100644
@@ -32,6 +32,7 @@
 #include <linux/module.h>
 #include <linux/start_kernel.h>
 #include <linux/memblock.h>
+#include <linux/of.h>
 #include <uapi/linux/mount.h>
 
 #include <asm/io.h>
@@ -466,25 +467,9 @@ static void __init report_hwcaps(unsigned long caps)
 		printk(KERN_CONT "]\n");
 }
 
-static unsigned long __init mdesc_cpu_hwcap_list(void)
+static unsigned long __init hwcap_list_caps(const char *prop, int len)
 {
-	struct mdesc_handle *hp;
 	unsigned long caps = 0;
-	const char *prop;
-	int len;
-	u64 pn;
-
-	hp = mdesc_grab();
-	if (!hp)
-		return 0;
-
-	pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "cpu");
-	if (pn == MDESC_NODE_NULL)
-		goto out;
-
-	prop = mdesc_get_property(hp, pn, "hwcap-list", &len);
-	if (!prop)
-		goto out;
 
 	while (len) {
 		int i, plen;
@@ -510,11 +495,53 @@ static unsigned long __init mdesc_cpu_hwcap_list(void)
 		len -= plen;
 	}
 
+	return caps;
+}
+
+static unsigned long __init mdesc_cpu_hwcap_list(void)
+{
+	struct mdesc_handle *hp;
+	unsigned long caps = 0;
+	const char *prop;
+	int len;
+	u64 pn;
+
+	hp = mdesc_grab();
+	if (!hp)
+		return 0;
+
+	pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "cpu");
+	if (pn == MDESC_NODE_NULL)
+		goto out;
+
+	prop = mdesc_get_property(hp, pn, "hwcap-list", &len);
+	if (prop)
+		caps = hwcap_list_caps(prop, len);
+
 out:
 	mdesc_release(hp);
 	return caps;
 }
 
+/* sun4u has no machine description; QEMU lists the caps here. */
+static unsigned long __init of_cpu_hwcap_list(void)
+{
+	struct device_node *dp;
+	unsigned long caps = 0;
+	const char *prop;
+	int len;
+
+	dp = of_find_node_by_type(NULL, "cpu");
+	if (!dp)
+		return 0;
+
+	prop = of_get_property(dp, "qemu,hwcap-list", &len);
+	if (prop)
+		caps = hwcap_list_caps(prop, len);
+	of_node_put(dp);
+	return caps;
+}
+
 /* This yields a mask that user programs can use to figure out what
  * instruction set this cpu supports.
  */
@@ -552,6 +579,8 @@ static void __init init_sparc64_elf_hwcap(void)
 	cap |= (AV_SPARC_MUL32 | AV_SPARC_DIV32 | AV_SPARC_V8PLUS);
 
 	mdesc_caps = mdesc_cpu_hwcap_list();
+	if (!mdesc_caps)
+		mdesc_caps = of_cpu_hwcap_list();
 	if (!mdesc_caps) {
 		if (tlb_type == spitfire)
 			cap |= AV_SPARC_VIS;
Signature · 1 line
2.43.0