All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u
@ 2026-11-17 11:29 Stian Halseth
  2026-11-17 11:46 ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Stian Halseth @ 2026-11-17 11:29 UTC (permalink / raw)
  To: sparclinux
  Cc: Andreas Larsson, David S . Miller, Eric Biggers, Mark Cave-Ayland,
	linux-kernel, 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.

Signed-off-by: Stian Halseth <stian@itx.no>
---
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/openbios@openbios.org/thread/3XVXVLHWT2T63IYWR67WPOHFSJGLP3NP/
Until QEMU ships an OpenBIOS with it, nvramrc can set the property.
This runs the crypto KUnit tests on the sparc64 code with QEMU master:

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'"

The sparc64 crypto drivers load and the KUnit suites pass. Without
+crypto the CPU caps are unchanged. Once a QEMU release has the
OpenBIOS side, kunit's sparc64 config could pass +crypto. Older QEMU
rejects that flag.

With +crypto, the self-tests of ecb-, cbc- and ctr-aes-sparc64 fail.
The glue keeps the AES round keys in FP registers across
skcipher_walk_done(). On sun4u, the memcpy there uses VIS and
overwrites them. No real sun4u CPU has the crypto opcodes, so this
only shows up under QEMU.

Moving sparc's ECB, CBC and CTR into lib/crypto, as for riscv and x86,
would fix it. Eric, is that something you plan to do?

 arch/sparc/kernel/setup_64.c | 63 ++++++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 17 deletions(-)

diff --git a/arch/sparc/kernel/setup_64.c b/arch/sparc/kernel/setup_64.c
index 63615f5c99b47..87e7b1bf4aeca 100644
--- a/arch/sparc/kernel/setup_64.c
+++ b/arch/sparc/kernel/setup_64.c
@@ -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;
@@ -507,11 +492,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.
  */
@@ -549,6 +576,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;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u
  2026-11-17 11:29 [PATCH] sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u Stian Halseth
@ 2026-11-17 11:46 ` sashiko-bot
  2026-11-17 12:44   ` Stian Halseth
  0 siblings, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-11-17 11:46 UTC (permalink / raw)
  To: Stian Halseth, sparclinux

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] sparc64: of_cpu_hwcap_list() enables broken sun4u skcipher FPU
  usage
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] sparc: infinite loop and out-of-bounds read in
  mdesc_cpu_hwcap_list() [1]
--

commit 2ca1536e272cd67392d1c65939db77b50052beab
Author: Stian Halseth <stian@itx.no>

sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u

This commit factors out the hwcap-list string parser and uses it to read
CPU capabilities from the OpenFirmware cpu node's qemu,hwcap-list property
when no sun4v machine description is present.

> diff --git a/arch/sparc/kernel/setup_64.c b/arch/sparc/kernel/setup_64.c
> index 48abee4eee29d..f63a05360150e 100644
> --- a/arch/sparc/kernel/setup_64.c
> +++ b/arch/sparc/kernel/setup_64.c

[ ... ]

> @@ -479,25 +480,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;
> @@ -520,11 +505,53 @@ static unsigned long __init mdesc_cpu_hwcap_list(void)
>  		len -= plen;
>  	}
>  
> +	return caps;
> +}

[Severity: High]
This problem wasn't introduced by this patch, but can hwcap_list_caps()
read out of bounds and loop forever [1] if the property buffer is not
null-terminated?

If the final entry in prop lacks a terminating NUL byte within len bytes,
strlen(prop) reads past the end of the property buffer and plen exceeds
len.  len -= plen then underflows the signed int len to a negative value,
so while (len) never terminates and hangs the boot.  Bounding the string
length with strnlen(prop, len) and checking while (len > 0) would avoid the
overread and underflow.

[ ... ]

> @@ -562,6 +589,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;

[Severity: High]
Does setting HWCAP_SPARC_CRYPTO in sparc64_elf_hwcap on sun4u cause a
regression where the sparc64 skcipher drivers corrupt FPU state and round
keys across skcipher_walk_done()?

When HWCAP_SPARC_CRYPTO is set, the sparc64 AES, DES, and Camellia skcipher
drivers in arch/sparc/crypto/ register at priority 300.  Their encryption
and decryption loops load expanded keys into FPU registers with FPRS_FEF
enabled once before the walk loop and keep them live across
skcipher_walk_done():

arch/sparc/crypto/aes_glue.c:ecb_encrypt() {
    ...
	ctx->ops->load_encrypt_keys(&ctx->key[0]);
	while ((nbytes = walk.nbytes) != 0) {
		ctx->ops->ecb_encrypt(&ctx->key[0], walk.src.virt.addr,
				      walk.dst.virt.addr,
				      round_down(nbytes, AES_BLOCK_SIZE));
		err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
	}
	fprs_write(0);
    ...
}

Unlike sun4v's NG4memcpy/M7memcpy (which use VISEntryHalfFast and fall back
to integer copies when FPRS_FEF is already set), sun4u uses U1memcpy or
U3memcpy, which unconditionally execute VISEntry/VISEntryHalf and VISExit
for copies above the VIS threshold.  When skcipher_walk_done() calls
memcpy() on unaligned or multi-step walks, VISenter saves the expanded
cipher round keys over the current task's user FPU state in
current_thread_info()->fpregs[0], and U1memcpy/U3memcpy overwrite the FPU
registers and clear FPRS_FEF, causing boot-time crypto self-test failures
and silent data or user FPU corruption.

[1] https://sashiko.dev/#/bug/linux-93078b7c-14ab-4a50-a56f-abb0c72ca6f5

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117112914.2300417-1-stian@itx.no?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u
  2026-11-17 11:46 ` sashiko-bot
@ 2026-11-17 12:44   ` Stian Halseth
  2026-11-18  4:38     ` Eric Biggers
  0 siblings, 1 reply; 4+ messages in thread
From: Stian Halseth @ 2026-11-17 12:44 UTC (permalink / raw)
  To: sashiko-reviews, sparclinux
  Cc: Andreas Larsson, David S . Miller, Eric Biggers, Mark Cave-Ayland,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1364 bytes --]

Hi all,

On Tue, 2026-11-17 at 11:46 +0000, sashiko-bot@kernel.org wrote:
> 
> [Severity: High]
> This problem wasn't introduced by this patch, but can hwcap_list_caps()
> read out of bounds and loop forever [1] if the property buffer is not
> null-terminated?

Yes, and we can fix that in v2. As it stands the parser trusts the list
to end with a NUL, which is not guaranteed if a QEMU user supplies a
fw_cfg file directly.

> 
> [Severity: High]
> Does setting HWCAP_SPARC_CRYPTO in sparc64_elf_hwcap on sun4u cause a
> regression where the sparc64 skcipher drivers corrupt FPU state and round
> keys across skcipher_walk_done()?
> 

This might be a bit worse than I first thought. I was aware of the
self-test failures, as noted in my patch notes, but it looks like this
could also cause some data corruption. Only QEMU can hit it, since no
real sun4u CPU has the crypto opcodes.

I have not reproduced it myself, and before spending time on that,
I would like to get Eric's take on removing the glue code altogether.
If not, we could fix the glue itself, for example by dropping the FP
state before skcipher_walk_done(). IMO that would be maintaining code
that probably should be removed. Not my call, though.

Andreas, please don't apply this patch as-is for now. I will wait for
Eric's reply before proceeding.


/Stian


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u
  2026-11-17 12:44   ` Stian Halseth
@ 2026-11-18  4:38     ` Eric Biggers
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2026-11-18  4:38 UTC (permalink / raw)
  To: Stian Halseth
  Cc: sashiko-reviews, sparclinux, Andreas Larsson, David S . Miller,
	Mark Cave-Ayland, linux-kernel

Hi Stian,

On Tue, Nov 17, 2026 at 01:44:14PM +0100, Stian Halseth wrote:
> Hi all,
> 
> On Tue, 2026-11-17 at 11:46 +0000, sashiko-bot@kernel.org wrote:
> > 
> > [Severity: High]
> > This problem wasn't introduced by this patch, but can hwcap_list_caps()
> > read out of bounds and loop forever [1] if the property buffer is not
> > null-terminated?
> 
> Yes, and we can fix that in v2. As it stands the parser trusts the list
> to end with a NUL, which is not guaranteed if a QEMU user supplies a
> fw_cfg file directly.
> 
> > 
> > [Severity: High]
> > Does setting HWCAP_SPARC_CRYPTO in sparc64_elf_hwcap on sun4u cause a
> > regression where the sparc64 skcipher drivers corrupt FPU state and round
> > keys across skcipher_walk_done()?
> > 
> 
> This might be a bit worse than I first thought. I was aware of the
> self-test failures, as noted in my patch notes, but it looks like this
> could also cause some data corruption. Only QEMU can hit it, since no
> real sun4u CPU has the crypto opcodes.
> 
> I have not reproduced it myself, and before spending time on that,
> I would like to get Eric's take on removing the glue code altogether.
> If not, we could fix the glue itself, for example by dropping the FP
> state before skcipher_walk_done(). IMO that would be maintaining code
> that probably should be removed. Not my call, though.
> 
> Andreas, please don't apply this patch as-is for now. I will wait for
> Eric's reply before proceeding.

The following series migrates the remaining SPARC crypto opcodes
accelerated AES modes into lib/crypto/:
https://lore.kernel.org/linux-crypto/20261118041033.50014-1-ebiggers@kernel.org/

There we have more control over the bounce buffering, and I set the size
to 128 bytes (rather than crypto/skcipher.c's PAGE_SIZE) to keep it
below the size threshold at which memcpy() uses the FPU registers.

I tested this patch with that series and the QEMU support you added, and
everything seems to work.  And Camellia is not supported in QEMU, so
that is not an issue either (and really it's time to just delete the
kernel's Camellia code anyway, as no one uses it).  So feel free to add:

    Tested-by: Eric Biggers <ebiggers@kernel.org>

We should be fine as long as my series is merged too (and I plan to take
it via libcrypto-next in 7.5).

I think this is the only case where a Linux architecture has
restrictions on when memcpy() can be called, though.  It would be worth
finding a way to remove that footgun, e.g. making memcpy() automatically
fall back to scalar code if the FPU is already in use.  It seems that
some of the SPARC memcpy() implementations already do that, in fact.

Thanks,

- Eric

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18  4:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 11:29 [PATCH] sparc64: Read qemu,hwcap-list from the OF cpu node on sun4u Stian Halseth
2026-11-17 11:46 ` sashiko-bot
2026-11-17 12:44   ` Stian Halseth
2026-11-18  4:38     ` Eric Biggers

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.