Skip to contentSkip to conversation

arm: boot/compressed: fix decompressor header layout for v7-M

PATCH New Ard Biesheuvel 5 participants 16 messages May 19, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
 arch/arm/boot/compressed/efi-header.S |  4 +---
 arch/arm/boot/compressed/head.S       | 14 +++++++-------
 2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
 		@ there.
 		.inst	'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-		mov	r0, r0
+		W(mov)	r0, r0
 #endif
 		.endm
 
 		.macro	__EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-		b	__efi_start
-
 		.set	start_offset, __efi_start - start
 		.org	start + 0x3c
 		@
arch/arm/boot/compressed/head.S +7 -7
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
 		.rept	7
 		__nop
 		.endr
-   ARM(		mov	r0, r0		)
-   ARM(		b	1f		)
- THUMB(		badr	r12, 1f		)
- THUMB(		bx	r12		)
+      ARM(	b	1f		)
+ AR_CLASS(	sub	pc, pc, #3	)	@ A/R: switch to Thumb2 mode
+  M_CLASS(	nop.w			)	@ M: already in Thumb2 mode
+    THUMB(	.thumb			)
+    THUMB(	b.w	1f		)
 
 		.word	_magic_sig	@ Magic numbers to help the loader
 		.word	_magic_start	@ absolute load/run zImage address
 		.word	_magic_end	@ zImage end address
 		.word	0x04030201	@ endianness flag
 
- THUMB(		.thumb			)
-1:		__EFI_HEADER
-
+		__EFI_HEADER
+1:
  ARM_BE8(	setend	be		)	@ go BE8 if compiled for BE8
  AR_CLASS(	mrs	r9, cpsr	)
 #ifdef CONFIG_ARM_VIRT_EXT
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 18 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

Quoted 2 lines
Quoted 60 lines
---
 arch/arm/boot/compressed/efi-header.S |  4 +---
 arch/arm/boot/compressed/head.S       | 14 +++++++-------
 2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
 		@ there.
 		.inst	'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-		mov	r0, r0
+		W(mov)	r0, r0
 #endif
 		.endm
 
 		.macro	__EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-		b	__efi_start
-
 		.set	start_offset, __efi_start - start
 		.org	start + 0x3c
 		@
arch/arm/boot/compressed/head.S +7 -7
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
 		.rept	7
 		__nop
 		.endr
-   ARM(		mov	r0, r0		)
-   ARM(		b	1f		)
- THUMB(		badr	r12, 1f		)
- THUMB(		bx	r12		)
+      ARM(	b	1f		)
+ AR_CLASS(	sub	pc, pc, #3	)	@ A/R: switch to Thumb2 mode
+  M_CLASS(	nop.w			)	@ M: already in Thumb2 mode
+    THUMB(	.thumb			)
+    THUMB(	b.w	1f		)
 
 		.word	_magic_sig	@ Magic numbers to help the loader
 		.word	_magic_start	@ absolute load/run zImage address
 		.word	_magic_end	@ zImage end address
 		.word	0x04030201	@ endianness flag
 
- THUMB(		.thumb			)
-1:		__EFI_HEADER
-
+		__EFI_HEADER
+1:
  ARM_BE8(	setend	be		)	@ go BE8 if compiled for BE8
  AR_CLASS(	mrs	r9, cpsr	)
 #ifdef CONFIG_ARM_VIRT_EXT
Signature · 1 line
2.9.3
#2 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

In reply to Ard Biesheuvel
Quoted 52 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
 arch/arm/boot/compressed/efi-header.S |  4 +---
 arch/arm/boot/compressed/head.S       | 14 +++++++-------
 2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
 		@ there.
 		.inst	'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-		mov	r0, r0
+		W(mov)	r0, r0
 #endif
 		.endm
 
 		.macro	__EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-		b	__efi_start
-
 		.set	start_offset, __efi_start - start
 		.org	start + 0x3c
 		@
arch/arm/boot/compressed/head.S +0 -0
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
 		.rept	7
 		__nop

arch/arm/boot/compressed/head.S start: · line 137

Quoted 10 lines
arch/arm/boot/compressed/head.S +5 -4
 		.endr
-   ARM(		mov	r0, r0		)
-   ARM(		b	1f		)
- THUMB(		badr	r12, 1f		)
- THUMB(		bx	r12		)
+      ARM(	b	1f		)
+ AR_CLASS(	sub	pc, pc, #3	)	@ A/R: switch to Thumb2 mode
+  M_CLASS(	nop.w			)	@ M: already in Thumb2 mode
+    THUMB(	.thumb			)
+    THUMB(	b.w	1f		)

It's not so easy to see that this always assembles to two words, or that the "sub pc, pc, #3" is assembled but not executed in an ARM kernel.

Spelling it out might be more readable:

ARM( mov r0, r0 )
ARM( mov r0, r0 )

THUMB( AR_CLASS( sub pc, pc, #3 ))
THUMB( M_CLASS( nop.w ))
THUMB( .thumb )
THUMB( b.w 1f )

But I guess it works either way.

Cheers
---Dave

Quoted 11 lines
arch/arm/boot/compressed/head.S +2 -3
 		.word	_magic_sig	@ Magic numbers to help the loader
 		.word	_magic_start	@ absolute load/run zImage address
 		.word	_magic_end	@ zImage end address
 		.word	0x04030201	@ endianness flag
 
- THUMB(		.thumb			)
-1:		__EFI_HEADER
-
+		__EFI_HEADER
+1:

[...]

#3 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 72 lines · Dave Martin
Quoted 52 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
 arch/arm/boot/compressed/efi-header.S |  4 +---
 arch/arm/boot/compressed/head.S       | 14 +++++++-------
 2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
              @ there.
              .inst   'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-             mov     r0, r0
+             W(mov)  r0, r0
 #endif
              .endm
 
              .macro  __EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-             b       __efi_start
-
              .set    start_offset, __efi_start - start
              .org    start + 0x3c
              @
arch/arm/boot/compressed/head.S +0 -0
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
              .rept   7
              __nop

arch/arm/boot/compressed/head.S start: · line 137

Quoted 10 lines
arch/arm/boot/compressed/head.S +5 -4
              .endr
-   ARM(              mov     r0, r0          )
-   ARM(              b       1f              )
- THUMB(              badr    r12, 1f         )
- THUMB(              bx      r12             )
+      ARM(   b       1f              )
+ AR_CLASS(   sub     pc, pc, #3      )       @ A/R: switch to Thumb2 mode
+  M_CLASS(   nop.w                   )       @ M: already in Thumb2 mode
+    THUMB(   .thumb                  )
+    THUMB(   b.w     1f              )

It's not so easy to see that this always assembles to two words, or that the "sub pc, pc, #3" is assembled but not executed in an ARM kernel.

Spelling it out might be more readable:

   ARM(                 mov     r0, r0          )
   ARM(                 mov     r0, r0          )
Quoted 7 lines
 THUMB( AR_CLASS(       sub     pc, pc, #3      ))
 THUMB( M_CLASS(        nop.w                   ))
 THUMB(                 .thumb                  )
 THUMB(                 b.w     1f              )

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

I could add some more comments as well if that helps?

#4 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 78 lines · Ard Biesheuvel
Quoted 77 lines · Dave Martin
Quoted 52 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
 arch/arm/boot/compressed/efi-header.S |  4 +---
 arch/arm/boot/compressed/head.S       | 14 +++++++-------
 2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
              @ there.
              .inst   'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-             mov     r0, r0
+             W(mov)  r0, r0
 #endif
              .endm
 
              .macro  __EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-             b       __efi_start
-
              .set    start_offset, __efi_start - start
              .org    start + 0x3c
              @
arch/arm/boot/compressed/head.S +0 -0
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
              .rept   7
              __nop

arch/arm/boot/compressed/head.S start: · line 137

Quoted 10 lines
arch/arm/boot/compressed/head.S +5 -4
              .endr
-   ARM(              mov     r0, r0          )
-   ARM(              b       1f              )
- THUMB(              badr    r12, 1f         )
- THUMB(              bx      r12             )
+      ARM(   b       1f              )
+ AR_CLASS(   sub     pc, pc, #3      )       @ A/R: switch to Thumb2 mode
+  M_CLASS(   nop.w                   )       @ M: already in Thumb2 mode
+    THUMB(   .thumb                  )
+    THUMB(   b.w     1f              )

It's not so easy to see that this always assembles to two words, or that the "sub pc, pc, #3" is assembled but not executed in an ARM kernel.

Spelling it out might be more readable:

   ARM(                 mov     r0, r0          )
   ARM(                 mov     r0, r0          )
 THUMB( AR_CLASS(       sub     pc, pc, #3      ))
 THUMB( M_CLASS(        nop.w                   ))
 THUMB(                 .thumb                  )
 THUMB(                 b.w     1f              )
Quoted 6 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

If we're getting to this level of complexity, then maybe going back to ifdefs will help rather than nesting the magic macros:

#ifndef CONFIG_THUMB2_KERNEL
		mov	r0, r0
		b	1f
#else
 AR_CLASS(	sub.w	pc, pc, #3	)
  M_CLASS(	nop.w			)
		.thumb
		b.w	1f

#endif

This looks pretty clear to me.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#5 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 103 lines · Russell King - ARM Linux
Quoted 80 lines · Ard Biesheuvel
Quoted 78 lines · Dave Martin
Quoted 53 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
arch/arm/boot/compressed/efi-header.S | 4 +---
arch/arm/boot/compressed/head.S | 14 +++++++-------

2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
             @ there.
             .inst   'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-             mov     r0, r0
+             W(mov)  r0, r0
 #endif
             .endm
 
             .macro  __EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-             b       __efi_start
-
             .set    start_offset, __efi_start - start
             .org    start + 0x3c
             @
arch/arm/boot/compressed/head.S +0 -0
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
             .rept   7
             __nop

arch/arm/boot/compressed/head.S start: · line 137

Quoted 10 lines
arch/arm/boot/compressed/head.S +5 -4
             .endr
-   ARM(              mov     r0, r0          )
-   ARM(              b       1f              )
- THUMB(              badr    r12, 1f         )
- THUMB(              bx      r12             )
+      ARM(   b       1f              )
+ AR_CLASS(   sub     pc, pc, #3      )       @ A/R: switch to Thumb2 mode
+  M_CLASS(   nop.w                   )       @ M: already in Thumb2 mode
+    THUMB(   .thumb                  )
+    THUMB(   b.w     1f              )

It's not so easy to see that this always assembles to two words, or that the "sub pc, pc, #3" is assembled but not executed in an ARM kernel.

Spelling it out might be more readable:

  ARM(                 mov     r0, r0          )
  ARM(                 mov     r0, r0          )
THUMB( AR_CLASS(       sub     pc, pc, #3      ))
THUMB( M_CLASS(        nop.w                   ))
THUMB(                 .thumb                  )
THUMB(                 b.w     1f              )
Quoted 6 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

If we're getting to this level of complexity, then maybe going back to ifdefs will help rather than nesting the magic macros:

#ifndef CONFIG_THUMB2_KERNEL
mov r0, r0
b 1f
#else
AR_CLASS( sub.w pc, pc, #3 )
M_CLASS( nop.w )
.thumb
b.w 1f
#endif

Quoted 1 line

This looks pretty clear to me.

Good point -- let me use that for v2

#6 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

In reply to Ard Biesheuvel
Quoted 78 lines · Ard Biesheuvel
Quoted 77 lines · Dave Martin
Quoted 52 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
 arch/arm/boot/compressed/efi-header.S |  4 +---
 arch/arm/boot/compressed/head.S       | 14 +++++++-------
 2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
              @ there.
              .inst   'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-             mov     r0, r0
+             W(mov)  r0, r0
 #endif
              .endm
 
              .macro  __EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-             b       __efi_start
-
              .set    start_offset, __efi_start - start
              .org    start + 0x3c
              @
arch/arm/boot/compressed/head.S +0 -0
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
              .rept   7
              __nop

arch/arm/boot/compressed/head.S start: · line 137

Quoted 10 lines
arch/arm/boot/compressed/head.S +5 -4
              .endr
-   ARM(              mov     r0, r0          )
-   ARM(              b       1f              )
- THUMB(              badr    r12, 1f         )
- THUMB(              bx      r12             )
+      ARM(   b       1f              )
+ AR_CLASS(   sub     pc, pc, #3      )       @ A/R: switch to Thumb2 mode
+  M_CLASS(   nop.w                   )       @ M: already in Thumb2 mode
+    THUMB(   .thumb                  )
+    THUMB(   b.w     1f              )

It's not so easy to see that this always assembles to two words, or that the "sub pc, pc, #3" is assembled but not executed in an ARM kernel.

Spelling it out might be more readable:

   ARM(                 mov     r0, r0          )
   ARM(                 mov     r0, r0          )
 THUMB( AR_CLASS(       sub     pc, pc, #3      ))
 THUMB( M_CLASS(        nop.w                   ))
 THUMB(                 .thumb                  )
 THUMB(                 b.w     1f              )
Quoted 8 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

I could add some more comments as well if that helps?

Hmm, couldn't it be ISA-agnostic?

ldr pc, 1f
.ltorg
THUMB( .thumb )

	...

given that that should be interworking on anything where THUMB() is valid in the first place.

Robin.

Quoted 6 lines

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#7 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 94 lines · Robin Murphy
Quoted 80 lines · Ard Biesheuvel
Quoted 78 lines · Dave Martin
Quoted 53 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
arch/arm/boot/compressed/efi-header.S | 4 +---
arch/arm/boot/compressed/head.S | 14 +++++++-------

2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
             @ there.
             .inst   'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-             mov     r0, r0
+             W(mov)  r0, r0
 #endif
             .endm
 
             .macro  __EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-             b       __efi_start
-
             .set    start_offset, __efi_start - start
             .org    start + 0x3c
             @
arch/arm/boot/compressed/head.S +0 -0
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
             .rept   7
             __nop

arch/arm/boot/compressed/head.S start: · line 137

Quoted 10 lines
arch/arm/boot/compressed/head.S +5 -4
             .endr
-   ARM(              mov     r0, r0          )
-   ARM(              b       1f              )
- THUMB(              badr    r12, 1f         )
- THUMB(              bx      r12             )
+      ARM(   b       1f              )
+ AR_CLASS(   sub     pc, pc, #3      )       @ A/R: switch to Thumb2 mode
+  M_CLASS(   nop.w                   )       @ M: already in Thumb2 mode
+    THUMB(   .thumb                  )
+    THUMB(   b.w     1f              )

It's not so easy to see that this always assembles to two words, or that the "sub pc, pc, #3" is assembled but not executed in an ARM kernel.

Spelling it out might be more readable:

  ARM(                 mov     r0, r0          )
  ARM(                 mov     r0, r0          )
THUMB( AR_CLASS(       sub     pc, pc, #3      ))
THUMB( M_CLASS(        nop.w                   ))
THUMB(                 .thumb                  )
THUMB(                 b.w     1f              )
Quoted 8 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

I could add some more comments as well if that helps?

Hmm, couldn't it be ISA-agnostic?

Quoted 8 lines

ldr pc, 1f
.ltorg
THUMB( .thumb )
...

given that that should be interworking on anything where THUMB() is valid in the first place.

The decompressor runs at a a priori unknown offset so literals containing absolute values are not helpful here.

Quoted 7 lines
Quoted 6 lines

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#8 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 99 lines · Ard Biesheuvel
Quoted 99 lines · Robin Murphy
Quoted 80 lines · Ard Biesheuvel
Quoted 78 lines · Dave Martin
Quoted 53 lines · Ard Biesheuvel

As reported by Patrice, the header layout of the decompressor is incorrect when building for v7-M. In this case, the __nop macro resolves to 'mov r0, r0', which is emitted as a narrow encoding, resulting in the header data fields to end up at lower offsets than required.

Given the variety of targets we need to support with the same code, the startup sequence is a bit of a jumble, and uses instructions and macros whose encoding width cannot be specified (badr), or only exists in a narrow encoding (bx)

So force the use of a wide encoding in __nop, and replace the start sequence with a simple jump to the label marking the start of code, preceded by a Thumb2 mode switch if required (using explicit wide encodings where appropriate). The label itself can be moved to the start of code [where it belongs] due to the larger range of branch instructions as compared to adr instructions.

---
arch/arm/boot/compressed/efi-header.S | 4 +---
arch/arm/boot/compressed/head.S | 14 +++++++-------

2 files changed, 8 insertions(+), 10 deletions(-)
arch/arm/boot/compressed/efi-header.S +1 -3
index 9d5dc4fda3c1..3f7d1b74c5e0 100644
@@ -17,14 +17,12 @@
             @ there.
             .inst   'M' | ('Z' << 8) | (0x1310 << 16)   @ tstne r0, #0x4d000
 #else
-             mov     r0, r0
+             W(mov)  r0, r0
 #endif
             .endm
 
             .macro  __EFI_HEADER
 #ifdef CONFIG_EFI_STUB
-             b       __efi_start
-
             .set    start_offset, __efi_start - start
             .org    start + 0x3c
             @
arch/arm/boot/compressed/head.S +0 -0
index 7c711ba61417..8c336a6da210 100644
@@ -130,19 +130,19 @@ start:
             .rept   7
             __nop

arch/arm/boot/compressed/head.S start: · line 137

Quoted 10 lines
arch/arm/boot/compressed/head.S +5 -4
             .endr
-   ARM(              mov     r0, r0          )
-   ARM(              b       1f              )
- THUMB(              badr    r12, 1f         )
- THUMB(              bx      r12             )
+      ARM(   b       1f              )
+ AR_CLASS(   sub     pc, pc, #3      )       @ A/R: switch to Thumb2 mode
+  M_CLASS(   nop.w                   )       @ M: already in Thumb2 mode
+    THUMB(   .thumb                  )
+    THUMB(   b.w     1f              )

It's not so easy to see that this always assembles to two words, or that the "sub pc, pc, #3" is assembled but not executed in an ARM kernel.

Spelling it out might be more readable:

  ARM(                 mov     r0, r0          )
  ARM(                 mov     r0, r0          )
THUMB( AR_CLASS(       sub     pc, pc, #3      ))
THUMB( M_CLASS(        nop.w                   ))
THUMB(                 .thumb                  )
THUMB(                 b.w     1f              )
Quoted 8 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

I could add some more comments as well if that helps?

Hmm, couldn't it be ISA-agnostic?

ldr pc, 1f
.ltorg
THUMB( .thumb )
...

Quoted 5 lines
Quoted 3 lines

given that that should be interworking on anything where THUMB() is valid in the first place.

The decompressor runs at a a priori unknown offset so literals containing absolute values are not helpful here.

Ah, I knew I'd have overlooked something - that makes sense.

FWIW, my final thought is this:

	add	r12, pc, #5
	mov	r0, r0
	mov	pc, r12

Which I think should leave you in Thumb regardless of which ISA it's assembled in, thanks to the wackiness of ALUWritePC().

Robin.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#9 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 10 lines · Robin Murphy

Ah, I knew I'd have overlooked something - that makes sense.

FWIW, my final thought is this:

	add	r12, pc, #5
	mov	r0, r0
	mov	pc, r12

Which I think should leave you in Thumb regardless of which ISA it's assembled in, thanks to the wackiness of ALUWritePC().

You only have four bytes to fit whatever code in here.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#10 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

In reply to Robin Murphy
Quoted 2 lines · Robin Murphy
Quoted 1 line · Ard Biesheuvel

On 19 May 2017 at 17:46, Dave Martin <[email protected]> wrote:

[...]

Quoted 21 lines
Quoted 10 lines
Quoted 10 lines

Spelling it out might be more readable:

   ARM(                 mov     r0, r0          )
   ARM(                 mov     r0, r0          )
 THUMB( AR_CLASS(       sub     pc, pc, #3      ))
 THUMB( M_CLASS(        nop.w                   ))
 THUMB(                 .thumb                  )
 THUMB(                 b.w     1f              )
Quoted 8 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

I could add some more comments as well if that helps?

Hmm, couldn't it be ISA-agnostic?

Quoted 7 lines

ldr pc, 1f
.ltorg
THUMB( .thumb )

	...

given that that should be interworking on anything where THUMB() is valid in the first place.

Did you mean

	ldr	pc, =BSYM(1f)

?

Otherwise this is quite neat: providing there are no pending literals when the assembler reaches the ldr pc, and providing that the ldr pc is word-aligned (it certainly should be) then that sequence should always expand to two words.

Cheers
---Dave

#11 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 1 line · Dave Martin

On Fri, May 19, 2017 at 06:16:36PM +0100, Robin Murphy wrote:

[...]

Quoted 12 lines
Quoted 9 lines

Hmm, couldn't it be ISA-agnostic?

ldr pc, 1f
.ltorg
THUMB( .thumb )

	...

given that that should be interworking on anything where THUMB() is valid in the first place.

Did you mean

Quoted 7 lines
	ldr	pc, =BSYM(1f)

?

Otherwise this is quite neat: providing there are no pending literals when the assembler reaches the ldr pc, and providing that the ldr pc is word-aligned (it certainly should be) then that sequence should always expand to two words.

Modulo Ard's comment about position-independence... Curses.

Cheers
---Dave

#12 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

In reply to Robin Murphy
Quoted 9 lines · Robin Murphy
Quoted 5 lines · Ard Biesheuvel

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

I could add some more comments as well if that helps?

Hmm, couldn't it be ISA-agnostic?

Quoted 7 lines

ldr pc, 1f
.ltorg
THUMB( .thumb )

	...

given that that should be interworking on anything where THUMB() is valid in the first place.

Unfortunately not, this is PIC code, so we must not have anything that contains an absolute address. LDR to branch is out of the question.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#13 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

In reply to Ard Biesheuvel
Quoted 1 line · Ard Biesheuvel

On 19 May 2017 at 17:46, Dave Martin <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 10 lines

Spelling it out might be more readable:

   ARM(                 mov     r0, r0          )
   ARM(                 mov     r0, r0          )
 THUMB( AR_CLASS(       sub     pc, pc, #3      ))
 THUMB( M_CLASS(        nop.w                   ))
 THUMB(                 .thumb                  )
 THUMB(                 b.w     1f              )
Quoted 6 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

Err, yes. Actually I means to write

	b	1f
	mov	r0, r0

(since executing that nop before branching is presumably pointless).

Cheers
---Dave

#14 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 22 lines · Dave Martin
Quoted 1 line · Ard Biesheuvel

On 19 May 2017 at 17:46, Dave Martin <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 10 lines

Spelling it out might be more readable:

   ARM(                 mov     r0, r0          )
   ARM(                 mov     r0, r0          )
 THUMB( AR_CLASS(       sub     pc, pc, #3      ))
 THUMB( M_CLASS(        nop.w                   ))
 THUMB(                 .thumb                  )
 THUMB(                 b.w     1f              )
Quoted 6 lines
Quoted 2 lines

But I guess it works either way.

Indeed. Apart from the error in the second line, this sequence should be equivalent, but the nested macro invocations don't make it clearer imo.

Quoted 6 lines

Err, yes. Actually I means to write

	b	1f
	mov	r0, r0

(since executing that nop before branching is presumably pointless).

That "nop" is part of the 8 nops at the start of the image, since the entry point (due to historical reasons) is at 0x20 into the image.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#15 lore raw

PATCH 1/1 arm: boot/compressed: fix decompressor header layout for v7-M

Quoted 1 line · Russell King - ARM Linux

On Fri, May 19, 2017 at 06:29:34PM +0100, Dave Martin wrote:

[...]

Quoted 9 lines
Quoted 6 lines

Err, yes. Actually I means to write

	b	1f
	mov	r0, r0

(since executing that nop before branching is presumably pointless).

That "nop" is part of the 8 nops at the start of the image, since the entry point (due to historical reasons) is at 0x20 into the image.

Ah yes.

Cheers
---Dave