All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] armv8: Fix TCR 64-bit writes
@ 2022-05-09 16:08 Andre Przywara
  2022-05-10  7:56 ` Peng Fan
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Andre Przywara @ 2022-05-09 16:08 UTC (permalink / raw)
  To: Tom Rini; +Cc: Simon Glass, u-boot, Balaji Anandapadmanaban, Peng Fan, Andy Yan

The AArch64 TCR_ELx register is a 64-bit register, and many newer
architecture features use bits in the upper half. So far U-Boot was
igorant of those bits, trying to leave them alone.
However, in an effort to set bit 31 to 1, it failed doing so, because
the compiler sign-extended "1 << 31", so that all bits[63:31] got set.

Older ARMv8.0 cores don't define anything dangerous up there, but newer
architecture revisions do, and setting all those bits will end badly:
=================
$ qemu-system-aarch64 -cpu max ....
U-Boot 2022.07-rc1 (May 09 2022 - 15:21:00 +0100)

DRAM:  1.5 GiB
=================  (hangs here)

Defining TCR_ELx_RSVD to "1U << 31" avoids the sign-extension, so all
upper bits stay at a safe 0 value. This means no more surprises when
U-Boot runs on a more capable CPU core.

Reported-by: Balaji Anandapadmanaban <Balaji.Anandapadmanaban@arm.com>
Signed-off-by: Andre Przywara <andre.przywara@arm.com>
---
 arch/arm/include/asm/armv8/mmu.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/arm/include/asm/armv8/mmu.h b/arch/arm/include/asm/armv8/mmu.h
index fc97c55114..c36b2cf5a5 100644
--- a/arch/arm/include/asm/armv8/mmu.h
+++ b/arch/arm/include/asm/armv8/mmu.h
@@ -99,9 +99,9 @@
 #define TCR_TG0_16K		(2 << 14)
 #define TCR_EPD1_DISABLE	(1 << 23)
 
-#define TCR_EL1_RSVD		(1 << 31)
-#define TCR_EL2_RSVD		(1 << 31 | 1 << 23)
-#define TCR_EL3_RSVD		(1 << 31 | 1 << 23)
+#define TCR_EL1_RSVD		(1U << 31)
+#define TCR_EL2_RSVD		(1U << 31 | 1 << 23)
+#define TCR_EL3_RSVD		(1U << 31 | 1 << 23)
 
 #ifndef __ASSEMBLY__
 static inline void set_ttbr_tcr_mair(int el, u64 table, u64 tcr, u64 attr)
-- 
2.25.1


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

* RE: [PATCH] armv8: Fix TCR 64-bit writes
  2022-05-09 16:08 [PATCH] armv8: Fix TCR 64-bit writes Andre Przywara
@ 2022-05-10  7:56 ` Peng Fan
  2022-05-28  3:18   ` Peter Collingbourne
  2022-06-03 19:48 ` Tom Rini
  2022-06-06 21:00 ` Mark Kettenis
  2 siblings, 1 reply; 6+ messages in thread
From: Peng Fan @ 2022-05-10  7:56 UTC (permalink / raw)
  To: Andre Przywara, Tom Rini
  Cc: Simon Glass, u-boot, Balaji Anandapadmanaban, Andy Yan

> Subject: [PATCH] armv8: Fix TCR 64-bit writes
> 
> The AArch64 TCR_ELx register is a 64-bit register, and many newer architecture
> features use bits in the upper half. So far U-Boot was igorant of those bits,
> trying to leave them alone.
> However, in an effort to set bit 31 to 1, it failed doing so, because the compiler
> sign-extended "1 << 31", so that all bits[63:31] got set.
> 
> Older ARMv8.0 cores don't define anything dangerous up there, but newer
> architecture revisions do, and setting all those bits will end badly:
> =================
> $ qemu-system-aarch64 -cpu max ....
> U-Boot 2022.07-rc1 (May 09 2022 - 15:21:00 +0100)
> 
> DRAM:  1.5 GiB
> =================  (hangs here)
> 
> Defining TCR_ELx_RSVD to "1U << 31" avoids the sign-extension, so all upper
> bits stay at a safe 0 value. This means no more surprises when U-Boot runs on a
> more capable CPU core.
> 
> Reported-by: Balaji Anandapadmanaban <Balaji.Anandapadmanaban@arm.com>
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>

Reviewed-by: Peng Fan <peng.fan@nxp.com>

> ---
>  arch/arm/include/asm/armv8/mmu.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/arm/include/asm/armv8/mmu.h
> b/arch/arm/include/asm/armv8/mmu.h
> index fc97c55114..c36b2cf5a5 100644
> --- a/arch/arm/include/asm/armv8/mmu.h
> +++ b/arch/arm/include/asm/armv8/mmu.h
> @@ -99,9 +99,9 @@
>  #define TCR_TG0_16K		(2 << 14)
>  #define TCR_EPD1_DISABLE	(1 << 23)
> 
> -#define TCR_EL1_RSVD		(1 << 31)
> -#define TCR_EL2_RSVD		(1 << 31 | 1 << 23)
> -#define TCR_EL3_RSVD		(1 << 31 | 1 << 23)
> +#define TCR_EL1_RSVD		(1U << 31)
> +#define TCR_EL2_RSVD		(1U << 31 | 1 << 23)
> +#define TCR_EL3_RSVD		(1U << 31 | 1 << 23)
> 
>  #ifndef __ASSEMBLY__
>  static inline void set_ttbr_tcr_mair(int el, u64 table, u64 tcr, u64 attr)
> --
> 2.25.1


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

* Re: [PATCH] armv8: Fix TCR 64-bit writes
  2022-05-10  7:56 ` Peng Fan
@ 2022-05-28  3:18   ` Peter Collingbourne
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Collingbourne @ 2022-05-28  3:18 UTC (permalink / raw)
  To: Peng Fan
  Cc: Andre Przywara, Tom Rini, Simon Glass, u-boot,
	Balaji Anandapadmanaban, Andy Yan

On Tue, May 10, 2022 at 12:56 AM Peng Fan <peng.fan@nxp.com> wrote:
>
> > Subject: [PATCH] armv8: Fix TCR 64-bit writes
> >
> > The AArch64 TCR_ELx register is a 64-bit register, and many newer architecture
> > features use bits in the upper half. So far U-Boot was igorant of those bits,
> > trying to leave them alone.
> > However, in an effort to set bit 31 to 1, it failed doing so, because the compiler
> > sign-extended "1 << 31", so that all bits[63:31] got set.
> >
> > Older ARMv8.0 cores don't define anything dangerous up there, but newer
> > architecture revisions do, and setting all those bits will end badly:
> > =================
> > $ qemu-system-aarch64 -cpu max ....
> > U-Boot 2022.07-rc1 (May 09 2022 - 15:21:00 +0100)
> >
> > DRAM:  1.5 GiB
> > =================  (hangs here)
> >
> > Defining TCR_ELx_RSVD to "1U << 31" avoids the sign-extension, so all upper
> > bits stay at a safe 0 value. This means no more surprises when U-Boot runs on a
> > more capable CPU core.
> >
> > Reported-by: Balaji Anandapadmanaban <Balaji.Anandapadmanaban@arm.com>
> > Signed-off-by: Andre Przywara <andre.przywara@arm.com>
>
> Reviewed-by: Peng Fan <peng.fan@nxp.com>

I ran into the same problem as you and developed the same fix. I was
about to send it out before I found your patch on the list.

Tested-by: Peter Collingbourne <pcc@google.com>
Reviewed-by: Peter Collingbourne <pcc@google.com>

You may also want to add:

Fixes: ad3d6e88a1a4 ("armv8/mmu: Set bits marked RES1 in TCR")

Peter

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

* Re: [PATCH] armv8: Fix TCR 64-bit writes
  2022-05-09 16:08 [PATCH] armv8: Fix TCR 64-bit writes Andre Przywara
  2022-05-10  7:56 ` Peng Fan
@ 2022-06-03 19:48 ` Tom Rini
  2022-06-06 21:00 ` Mark Kettenis
  2 siblings, 0 replies; 6+ messages in thread
From: Tom Rini @ 2022-06-03 19:48 UTC (permalink / raw)
  To: Andre Przywara
  Cc: Simon Glass, u-boot, Balaji Anandapadmanaban, Peng Fan, Andy Yan

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

On Mon, May 09, 2022 at 05:08:49PM +0100, Andre Przywara wrote:

> The AArch64 TCR_ELx register is a 64-bit register, and many newer
> architecture features use bits in the upper half. So far U-Boot was
> igorant of those bits, trying to leave them alone.
> However, in an effort to set bit 31 to 1, it failed doing so, because
> the compiler sign-extended "1 << 31", so that all bits[63:31] got set.
> 
> Older ARMv8.0 cores don't define anything dangerous up there, but newer
> architecture revisions do, and setting all those bits will end badly:
> =================
> $ qemu-system-aarch64 -cpu max ....
> U-Boot 2022.07-rc1 (May 09 2022 - 15:21:00 +0100)
> 
> DRAM:  1.5 GiB
> =================  (hangs here)
> 
> Defining TCR_ELx_RSVD to "1U << 31" avoids the sign-extension, so all
> upper bits stay at a safe 0 value. This means no more surprises when
> U-Boot runs on a more capable CPU core.
> 
> Reported-by: Balaji Anandapadmanaban <Balaji.Anandapadmanaban@arm.com>
> Signed-off-by: Andre Przywara <andre.przywara@arm.com>
> Reviewed-by: Peng Fan <peng.fan@nxp.com>
> Tested-by: Peter Collingbourne <pcc@google.com>
> Reviewed-by: Peter Collingbourne <pcc@google.com>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

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

* Re: [PATCH] armv8: Fix TCR 64-bit writes
  2022-05-09 16:08 [PATCH] armv8: Fix TCR 64-bit writes Andre Przywara
  2022-05-10  7:56 ` Peng Fan
  2022-06-03 19:48 ` Tom Rini
@ 2022-06-06 21:00 ` Mark Kettenis
  2022-06-07 13:02   ` Andre Przywara
  2 siblings, 1 reply; 6+ messages in thread
From: Mark Kettenis @ 2022-06-06 21:00 UTC (permalink / raw)
  To: Andre Przywara
  Cc: trini, sjg, u-boot, Balaji.Anandapadmanaban, peng.fan, andy.yan

> From: Andre Przywara <andre.przywara@arm.com>
> Date: Mon,  9 May 2022 17:08:49 +0100
> 
> The AArch64 TCR_ELx register is a 64-bit register, and many newer
> architecture features use bits in the upper half. So far U-Boot was
> igorant of those bits, trying to leave them alone.
> However, in an effort to set bit 31 to 1, it failed doing so, because
> the compiler sign-extended "1 << 31", so that all bits[63:31] got set.
> 
> Older ARMv8.0 cores don't define anything dangerous up there, but newer
> architecture revisions do, and setting all those bits will end badly:
> =================
> $ qemu-system-aarch64 -cpu max ....
> U-Boot 2022.07-rc1 (May 09 2022 - 15:21:00 +0100)
> 
> DRAM:  1.5 GiB
> =================  (hangs here)
> 
> Defining TCR_ELx_RSVD to "1U << 31" avoids the sign-extension, so all
> upper bits stay at a safe 0 value. This means no more surprises when
> U-Boot runs on a more capable CPU core.

Hi Andre,

This change breaks U-Boot on Apple M1 systems.  Th reason is
"interesting".  The Apple Icestorm and Firestorm cores implement
FEAT_VHE, but do so in an unusual (non-compiant?) way.  the
HCR_EL2.E2H bit is hardwired to 1, which means that EL2 Host is always
enabled.  As a consequence, TCR_EL2 has a different layout (the same
as TCR_EL1).  This means that the get_tcr() function in
arch/arm/cpu/armv8/cache_v8.c returns the wrong value.  Apparently the
sign-extension that happened before accidentally set the "right" bits
so I didn't notice this issue before.

Below is a possible fix.  This uses #ifdef CONFIG_ARCH_APPLE to avoid
growing the code.  A more generic way of fixing this would involve
checking the value of HCR_EL2.E2H, probably in mmu_setup() and pass
el=1 instead of el=2 to get_tcr() instead.

Thoughts?


diff --git a/arch/arm/cpu/armv8/cache_v8.c b/arch/arm/cpu/armv8/cache_v8.c
index 3de18c7675..ea3a60b2cd 100644
--- a/arch/arm/cpu/armv8/cache_v8.c
+++ b/arch/arm/cpu/armv8/cache_v8.c
@@ -74,7 +74,11 @@ u64 get_tcr(int el, u64 *pips, u64 *pva_bits)
 	if (el == 1) {
 		tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
 	} else if (el == 2) {
+#ifdef CONFIG_ARCH_APPLE
+		tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
+#else
 		tcr = TCR_EL2_RSVD | (ips << 16);
+#endif
 	} else {
 		tcr = TCR_EL3_RSVD | (ips << 16);
 	}



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

* Re: [PATCH] armv8: Fix TCR 64-bit writes
  2022-06-06 21:00 ` Mark Kettenis
@ 2022-06-07 13:02   ` Andre Przywara
  0 siblings, 0 replies; 6+ messages in thread
From: Andre Przywara @ 2022-06-07 13:02 UTC (permalink / raw)
  To: Mark Kettenis
  Cc: trini, sjg, u-boot, Balaji.Anandapadmanaban, peng.fan, andy.yan

On Mon, 6 Jun 2022 23:00:08 +0200 (CEST)
Mark Kettenis <mark.kettenis@xs4all.nl> wrote:

Hi Mark,

> > From: Andre Przywara <andre.przywara@arm.com>
> > Date: Mon,  9 May 2022 17:08:49 +0100
> > 
> > The AArch64 TCR_ELx register is a 64-bit register, and many newer
> > architecture features use bits in the upper half. So far U-Boot was
> > igorant of those bits, trying to leave them alone.
> > However, in an effort to set bit 31 to 1, it failed doing so, because
> > the compiler sign-extended "1 << 31", so that all bits[63:31] got set.
> > 
> > Older ARMv8.0 cores don't define anything dangerous up there, but newer
> > architecture revisions do, and setting all those bits will end badly:
> > =================
> > $ qemu-system-aarch64 -cpu max ....
> > U-Boot 2022.07-rc1 (May 09 2022 - 15:21:00 +0100)
> > 
> > DRAM:  1.5 GiB
> > =================  (hangs here)
> > 
> > Defining TCR_ELx_RSVD to "1U << 31" avoids the sign-extension, so all
> > upper bits stay at a safe 0 value. This means no more surprises when
> > U-Boot runs on a more capable CPU core.  
> 
> Hi Andre,
> 
> This change breaks U-Boot on Apple M1 systems.  Th reason is
> "interesting".  The Apple Icestorm and Firestorm cores implement
> FEAT_VHE, but do so in an unusual (non-compiant?) way.

Ah, the good ol' always VHE hack, biting us over and over again.

> the
> HCR_EL2.E2H bit is hardwired to 1, which means that EL2 Host is always
> enabled.  As a consequence, TCR_EL2 has a different layout (the same
> as TCR_EL1).  This means that the get_tcr() function in
> arch/arm/cpu/armv8/cache_v8.c returns the wrong value.  Apparently the
> sign-extension that happened before accidentally set the "right" bits
> so I didn't notice this issue before.
> 
> Below is a possible fix.  This uses #ifdef CONFIG_ARCH_APPLE to avoid
> growing the code.  A more generic way of fixing this would involve
> checking the value of HCR_EL2.E2H, probably in mmu_setup() and pass
> el=1 instead of el=2 to get_tcr() instead.
> 
> Thoughts?

I am very much in favour of fixing this properly, so by using runtime VHE
detection.

Looking at the code, the "el" parameter in get_tcr is actually mostly just
the current EL, except in two cases, where we pass 0 (which looks wrong),
and are just after the VA size bits.

I will try to just determine the correct EL internally, so calling
current_el() and checking HCR_EL2.

Will send a patch ASAP.

Cheers,
Andre

> diff --git a/arch/arm/cpu/armv8/cache_v8.c b/arch/arm/cpu/armv8/cache_v8.c
> index 3de18c7675..ea3a60b2cd 100644
> --- a/arch/arm/cpu/armv8/cache_v8.c
> +++ b/arch/arm/cpu/armv8/cache_v8.c
> @@ -74,7 +74,11 @@ u64 get_tcr(int el, u64 *pips, u64 *pva_bits)
>  	if (el == 1) {
>  		tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
>  	} else if (el == 2) {
> +#ifdef CONFIG_ARCH_APPLE
> +		tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
> +#else
>  		tcr = TCR_EL2_RSVD | (ips << 16);
> +#endif
>  	} else {
>  		tcr = TCR_EL3_RSVD | (ips << 16);
>  	}
> 
> 


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

end of thread, other threads:[~2022-06-07 13:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-09 16:08 [PATCH] armv8: Fix TCR 64-bit writes Andre Przywara
2022-05-10  7:56 ` Peng Fan
2022-05-28  3:18   ` Peter Collingbourne
2022-06-03 19:48 ` Tom Rini
2022-06-06 21:00 ` Mark Kettenis
2022-06-07 13:02   ` Andre Przywara

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.