All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: nickc@redhat.com, binutils@sourceware.org,
	linux-arm-kernel@lists.infradead.org, linux@armlinux.org.uk,
	linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>, stable@vger.kernel.org
Subject: [PATCH] arm: detect buggy binutils when in thumb2 mode
Date: Thu, 23 Nov 2017 12:48:02 +0100	[thread overview]
Message-ID: <20171123114802.24942-1-Jason@zx2c4.com> (raw)
In-Reply-To: <CAHmME9qBxgDV-HGeqj75ohr=4Bf+TD73PcC0DUqTRh5PTOKyhQ@mail.gmail.com>

On older versions of binutils, \sym points to an aligned address. On
newer versions of binutils, \sym sometimes points to the unaligned thumb
address in certain circumstances. In order to homogenize this behavior,
rather than adding 1, we could simply OR in 1, so that already unaligned
instructions don't change. While that works, the downside is that we have
to add an `orr` instruction to a fast path. The assembler can't do this at
assemble time via "|1" because "invalid operands (.text and *ABS* sections)
for `|'". A better solution would be to have consistent binutils behavior,
but that ship has sailed.

So, this commit adds a detection mechanism, which began as a small thing
from Russell King that I then rewrote to use pure bash instead of
shelling out, so that it doesn't slow down the build process. The detection
mechanism _could_ be used to modify the assembly we generate, but for now
it's just being used to catch buggy binutils and abort the build process in
that case.

The rest of this commit message contains all of the relevant information
about the boot bug when compiled in thumb2 mode.

My tests concerned these versions:
broken: GNU ld (Gentoo 2.29.1 p3) 2.29.1
working: GNU ld (GNU Binutils for Ubuntu) 2.26.1

These produced the following code:
--- broken      2017-11-21 17:44:14.523416082 +0100
+++ working     2017-11-21 17:44:44.548461234 +0100
@@ -133,7 +133,7 @@
160:  f01a 0ff0       tst.w   sl, #240        ; 0xf0
164:  d111            bne.n   18a <__sys_trace>
166:  f5b7 7fc8       cmp.w   r7, #400        ; 0x190
- 16a:  f2af 1e6a       subw    lr, pc, #362    ; 0x16a
+ 16a:  f2af 1e6b       subw    lr, pc, #363    ; 0x16b
16e:  bf38            it      cc
170:  f858 f027       ldrcc.w pc, [r8, r7, lsl #2]
174:  a902            add     r1, sp, #8

The differing instruction corresponds with this actual line in
arch/arm/kernel/entry-common.S:
  badr    lr, ret_fast_syscall            @ return address

Running the broken kernel results in a runtime OOPS with:
PC is at ret_fast_syscall+0x4/0x52
LR is at ret_fast_syscall+0x2/0x52

The disassembly of that function for the crashing kernel is:
.text:00000000 ret_fast_syscall                        ; CODE XREF: sys_syscall+1C↓j
.text:00000000                 CPSID           I       ; jumptable 00000840 cases 15,18-376
.text:00000002
.text:00000002 loc_2                                   ; DATA XREF: sys_syscall-6BA↓o
.text:00000002                 LDR.W           R2, [R9,#8]
.text:00000006                 CMP.W           R2, #0xBF000000

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Russell King <linux@armlinux.org.uk>
Cc: nickc@redhat.com
Cc: stable@vger.kernel.org
---
 arch/arm/Makefile        |  7 +++++--
 arch/arm/tools/Makefile  |  5 ++++-
 arch/arm/tools/toolcheck | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/tools/toolcheck

diff --git a/arch/arm/Makefile b/arch/arm/Makefile
index 80351e505fd5..bd4e248a7f8f 100644
--- a/arch/arm/Makefile
+++ b/arch/arm/Makefile
@@ -319,16 +319,19 @@ all:	$(notdir $(KBUILD_IMAGE)) $(KBUILD_DTBS)
 archheaders:
 	$(Q)$(MAKE) $(build)=arch/arm/tools uapi
 
-archprepare:
+archprepare: toolcheck
 	$(Q)$(MAKE) $(build)=arch/arm/tools kapi
 
+toolcheck:
+	$(Q)$(MAKE) $(build)=arch/arm/tools $@
+
 # Convert bzImage to zImage
 bzImage: zImage
 
 BOOT_TARGETS	= zImage Image xipImage bootpImage uImage
 INSTALL_TARGETS	= zinstall uinstall install
 
-PHONY += bzImage $(BOOT_TARGETS) $(INSTALL_TARGETS)
+PHONY += bzImage $(BOOT_TARGETS) $(INSTALL_TARGETS) toolcheck
 
 bootpImage uImage: zImage
 zImage: Image
diff --git a/arch/arm/tools/Makefile b/arch/arm/tools/Makefile
index ddb89a7db36f..0a283756f1c5 100644
--- a/arch/arm/tools/Makefile
+++ b/arch/arm/tools/Makefile
@@ -23,12 +23,15 @@ uapi-hdrs-y += $(uapi)/unistd-eabi.h
 
 targets += $(addprefix ../../../,$(gen-y) $(kapi-hdrs-y) $(uapi-hdrs-y))
 
-PHONY += kapi uapi
+PHONY += kapi uapi toolcheck
 
 kapi:	$(kapi-hdrs-y) $(gen-y)
 
 uapi:	$(uapi-hdrs-y)
 
+toolcheck:
+	@'$(srctree)/$(src)/toolcheck'
+
 # Create output directory if not already present
 _dummy := $(shell [ -d '$(kapi)' ] || mkdir -p '$(kapi)') \
           $(shell [ -d '$(uapi)' ] || mkdir -p '$(uapi)')
diff --git a/arch/arm/tools/toolcheck b/arch/arm/tools/toolcheck
new file mode 100644
index 000000000000..04fc44b750d2
--- /dev/null
+++ b/arch/arm/tools/toolcheck
@@ -0,0 +1,44 @@
+#!/bin/bash
+#
+# Copyright 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+#
+
+set -e
+
+cleanup() {
+	[[ ! -d $temp ]] || rm -rf "$temp"
+	exit
+}
+trap cleanup INT TERM EXIT
+temp="$(mktemp -d)"
+
+check_thumb2_address() {
+	local disassembly
+
+	$CC $KBUILD_AFLAGS -o "$temp/a.out" -c -xassembler - <<-_EOF
+		.syntax unified
+		.thumb
+		.macro	badr, reg, sym
+		adr	\reg, \sym + 1
+		.endm
+
+		.type test, %function
+		.thumb_func
+		test:
+		mov	r0, #0
+		badr	lr, test
+	_EOF
+	disassembly="$($OBJDUMP -d "$temp/a.out")"
+
+	[[ $disassembly =~ 4:[[:space:]]*f2af\ 0e07 ]] && return 0
+
+	echo "Error: your assembler version produces buggy kernels:" >&2
+	read < <($AS --version) && echo "$REPLY" >&2
+	[[ $disassembly =~ 4:[[:space:]].*$ ]] && echo "${BASH_REMATCH[0]}" >&2 || echo "$disassembly" >&2
+	return 1
+}
+
+config="$(< .config)"
+[[ $config == *CONFIG_THUMB2_KERNEL=y* ]] && check_thumb2_address
+
+exit 0
-- 
2.15.0

WARNING: multiple messages have this Message-ID (diff)
From: Jason@zx2c4.com (Jason A. Donenfeld)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH] arm: detect buggy binutils when in thumb2 mode
Date: Thu, 23 Nov 2017 12:48:02 +0100	[thread overview]
Message-ID: <20171123114802.24942-1-Jason@zx2c4.com> (raw)
In-Reply-To: <CAHmME9qBxgDV-HGeqj75ohr=4Bf+TD73PcC0DUqTRh5PTOKyhQ@mail.gmail.com>

On older versions of binutils, \sym points to an aligned address. On
newer versions of binutils, \sym sometimes points to the unaligned thumb
address in certain circumstances. In order to homogenize this behavior,
rather than adding 1, we could simply OR in 1, so that already unaligned
instructions don't change. While that works, the downside is that we have
to add an `orr` instruction to a fast path. The assembler can't do this at
assemble time via "|1" because "invalid operands (.text and *ABS* sections)
for `|'". A better solution would be to have consistent binutils behavior,
but that ship has sailed.

So, this commit adds a detection mechanism, which began as a small thing
from Russell King that I then rewrote to use pure bash instead of
shelling out, so that it doesn't slow down the build process. The detection
mechanism _could_ be used to modify the assembly we generate, but for now
it's just being used to catch buggy binutils and abort the build process in
that case.

The rest of this commit message contains all of the relevant information
about the boot bug when compiled in thumb2 mode.

My tests concerned these versions:
broken: GNU ld (Gentoo 2.29.1 p3) 2.29.1
working: GNU ld (GNU Binutils for Ubuntu) 2.26.1

These produced the following code:
--- broken      2017-11-21 17:44:14.523416082 +0100
+++ working     2017-11-21 17:44:44.548461234 +0100
@@ -133,7 +133,7 @@
160:  f01a 0ff0       tst.w   sl, #240        ; 0xf0
164:  d111            bne.n   18a <__sys_trace>
166:  f5b7 7fc8       cmp.w   r7, #400        ; 0x190
- 16a:  f2af 1e6a       subw    lr, pc, #362    ; 0x16a
+ 16a:  f2af 1e6b       subw    lr, pc, #363    ; 0x16b
16e:  bf38            it      cc
170:  f858 f027       ldrcc.w pc, [r8, r7, lsl #2]
174:  a902            add     r1, sp, #8

The differing instruction corresponds with this actual line in
arch/arm/kernel/entry-common.S:
  badr    lr, ret_fast_syscall            @ return address

Running the broken kernel results in a runtime OOPS with:
PC is at ret_fast_syscall+0x4/0x52
LR is at ret_fast_syscall+0x2/0x52

The disassembly of that function for the crashing kernel is:
.text:00000000 ret_fast_syscall                        ; CODE XREF: sys_syscall+1C?j
.text:00000000                 CPSID           I       ; jumptable 00000840 cases 15,18-376
.text:00000002
.text:00000002 loc_2                                   ; DATA XREF: sys_syscall-6BA?o
.text:00000002                 LDR.W           R2, [R9,#8]
.text:00000006                 CMP.W           R2, #0xBF000000

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Cc: Russell King <linux@armlinux.org.uk>
Cc: nickc at redhat.com
Cc: stable at vger.kernel.org
---
 arch/arm/Makefile        |  7 +++++--
 arch/arm/tools/Makefile  |  5 ++++-
 arch/arm/tools/toolcheck | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/tools/toolcheck

diff --git a/arch/arm/Makefile b/arch/arm/Makefile
index 80351e505fd5..bd4e248a7f8f 100644
--- a/arch/arm/Makefile
+++ b/arch/arm/Makefile
@@ -319,16 +319,19 @@ all:	$(notdir $(KBUILD_IMAGE)) $(KBUILD_DTBS)
 archheaders:
 	$(Q)$(MAKE) $(build)=arch/arm/tools uapi
 
-archprepare:
+archprepare: toolcheck
 	$(Q)$(MAKE) $(build)=arch/arm/tools kapi
 
+toolcheck:
+	$(Q)$(MAKE) $(build)=arch/arm/tools $@
+
 # Convert bzImage to zImage
 bzImage: zImage
 
 BOOT_TARGETS	= zImage Image xipImage bootpImage uImage
 INSTALL_TARGETS	= zinstall uinstall install
 
-PHONY += bzImage $(BOOT_TARGETS) $(INSTALL_TARGETS)
+PHONY += bzImage $(BOOT_TARGETS) $(INSTALL_TARGETS) toolcheck
 
 bootpImage uImage: zImage
 zImage: Image
diff --git a/arch/arm/tools/Makefile b/arch/arm/tools/Makefile
index ddb89a7db36f..0a283756f1c5 100644
--- a/arch/arm/tools/Makefile
+++ b/arch/arm/tools/Makefile
@@ -23,12 +23,15 @@ uapi-hdrs-y += $(uapi)/unistd-eabi.h
 
 targets += $(addprefix ../../../,$(gen-y) $(kapi-hdrs-y) $(uapi-hdrs-y))
 
-PHONY += kapi uapi
+PHONY += kapi uapi toolcheck
 
 kapi:	$(kapi-hdrs-y) $(gen-y)
 
 uapi:	$(uapi-hdrs-y)
 
+toolcheck:
+	@'$(srctree)/$(src)/toolcheck'
+
 # Create output directory if not already present
 _dummy := $(shell [ -d '$(kapi)' ] || mkdir -p '$(kapi)') \
           $(shell [ -d '$(uapi)' ] || mkdir -p '$(uapi)')
diff --git a/arch/arm/tools/toolcheck b/arch/arm/tools/toolcheck
new file mode 100644
index 000000000000..04fc44b750d2
--- /dev/null
+++ b/arch/arm/tools/toolcheck
@@ -0,0 +1,44 @@
+#!/bin/bash
+#
+# Copyright 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+#
+
+set -e
+
+cleanup() {
+	[[ ! -d $temp ]] || rm -rf "$temp"
+	exit
+}
+trap cleanup INT TERM EXIT
+temp="$(mktemp -d)"
+
+check_thumb2_address() {
+	local disassembly
+
+	$CC $KBUILD_AFLAGS -o "$temp/a.out" -c -xassembler - <<-_EOF
+		.syntax unified
+		.thumb
+		.macro	badr, reg, sym
+		adr	\reg, \sym + 1
+		.endm
+
+		.type test, %function
+		.thumb_func
+		test:
+		mov	r0, #0
+		badr	lr, test
+	_EOF
+	disassembly="$($OBJDUMP -d "$temp/a.out")"
+
+	[[ $disassembly =~ 4:[[:space:]]*f2af\ 0e07 ]] && return 0
+
+	echo "Error: your assembler version produces buggy kernels:" >&2
+	read < <($AS --version) && echo "$REPLY" >&2
+	[[ $disassembly =~ 4:[[:space:]].*$ ]] && echo "${BASH_REMATCH[0]}" >&2 || echo "$disassembly" >&2
+	return 1
+}
+
+config="$(< .config)"
+[[ $config == *CONFIG_THUMB2_KERNEL=y* ]] && check_thumb2_address
+
+exit 0
-- 
2.15.0

  reply	other threads:[~2017-11-23 11:48 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-21 17:27 [PATCH] arm: ensure symbol is a thumb symbol in new binutils Jason A. Donenfeld
2017-11-21 17:27 ` Jason A. Donenfeld
2017-11-21 17:38 ` Russell King - ARM Linux
2017-11-21 17:38   ` Russell King - ARM Linux
2017-11-21 17:46   ` Jason A. Donenfeld
2017-11-21 17:46     ` Jason A. Donenfeld
2017-11-21 17:49     ` Russell King - ARM Linux
2017-11-21 17:49       ` Russell King - ARM Linux
2017-11-22 23:34       ` Jason A. Donenfeld
2017-11-22 23:34         ` Jason A. Donenfeld
2017-11-23 10:35         ` Russell King - ARM Linux
2017-11-23 10:35           ` Russell King - ARM Linux
2017-11-23 10:47           ` Fwd: " Jason A. Donenfeld
2017-11-23 10:47             ` Jason A. Donenfeld
2017-11-23 11:48             ` Jason A. Donenfeld [this message]
2017-11-23 11:48               ` [PATCH] arm: detect buggy binutils when in thumb2 mode Jason A. Donenfeld
2017-11-23 11:50               ` [PATCH v2] " Jason A. Donenfeld
2017-11-23 11:50                 ` Jason A. Donenfeld
2017-11-23 12:01                 ` Martin Storsjö
2017-11-23 12:01                   ` Martin Storsjö
2017-11-23 14:02             ` Fwd: [PATCH] arm: ensure symbol is a thumb symbol in new binutils Russell King - ARM Linux
2017-11-23 14:02               ` Russell King - ARM Linux
2017-11-23 14:26               ` Ard Biesheuvel
2017-11-23 14:26                 ` Ard Biesheuvel
2017-11-23 14:26                 ` Ard Biesheuvel
     [not found]                 ` <765227b5-981d-0cea-c831-73cfe2f58721@redhat.com>
     [not found]                   ` <aaf57bb5-13e6-852c-0f67-f72aedef0e79@gmail.com>
     [not found]                     ` <254af731-459b-1f1d-2d93-27c5a91e7bfb@redhat.com>
2018-05-31 21:16                       ` [Buildroot] " Romain Naour
2018-06-01  8:28                         ` Christophe PRIOUZEAU
     [not found]                           ` <2a8151c1-644e-a6b0-9a21-2c570ac7000b@redhat.com>
2018-06-05  8:52                             ` Christophe PRIOUZEAU
     [not found]                               ` <ec2b0134-6c4a-53ff-4ae2-69c3f0646ded@redhat.com>
2018-06-05 14:11                                 ` Christophe PRIOUZEAU
     [not found]                                   ` <1d8f302b-5ece-9921-b930-8c4bca61743e@redhat.com>
2018-06-11 14:47                                     ` Christophe PRIOUZEAU
     [not found]                                   ` <1d8f302b-5ece-9921-b930-8c4bca61743e@redhat. com>
     [not found]                                     ` <CAJA7tRbsNa7oTS-1sLs0WpA74RZr0R-v=sCyQxbDam5Gb7fYHA@mail.gmail.com>
     [not found]                                       ` <be2db8d4-7c12-2706-5084-322aa659c3bc@redhat.com>
2018-06-18 15:04                                         ` Christophe PRIOUZEAU
2018-06-18 18:29                                           ` Romain Naour
     [not found]                                             ` <062a421f-a669-fc1c-91e0-88cf73794414@redhat.com>
2018-06-19 12:27                                               ` Christophe PRIOUZEAU
2018-01-16  8:43 ` Chen-Yu Tsai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171123114802.24942-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=binutils@sourceware.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=nickc@redhat.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.