From patchwork Thu Nov 23 11:50:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 840737 Return-Path: X-Original-To: incoming-imx@patchwork.ozlabs.org Delivered-To: patchwork-incoming-imx@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=65.50.211.133; helo=bombadil.infradead.org; envelope-from=linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="SRVNVVgA"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=zx2c4.com header.i=@zx2c4.com header.b="GUP5J6og"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yjHhw07Kgz9s83 for ; Thu, 23 Nov 2017 22:51:12 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=XUZZkzzhj4NA0z81tfbxSoolME79GoC1G+AtlLrjnNI=; b=SRVNVVgAqW4P5B T/dzcF2iVgcGlpo6iD9C0ekrhxlRdB7Pr+7fcJgQ+E4XmuZhgqZTpWaZwAx+K+cARjm+7KH3QGflm BqT8Gw5kBZL3+5DBzzeOTe/RekisxenvqLTgsupnM7Fuo1EsEFgNDYodUB0XEBmw41ANjYMPjRNDz hncQwhv6g4NY+5skpAWxp/tcuP/8kDB/U35WrZUJQVlbaRzfES+SkUq6x6F9feWivnQ6etbfU1SNb uvLl9yS3Sp1kW009ONsDQkngi2TrDez0L1G2khk7Zuxpb324KIrqzKmPE5G4mwIYqaSiVdONXjA0/ c34ALAOfJr3g3PD++odg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1eHq2A-00023L-GW; Thu, 23 Nov 2017 11:51:10 +0000 Received: from frisell.zx2c4.com ([192.95.5.64]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1eHq1l-00020o-F6 for linux-arm-kernel@lists.infradead.org; Thu, 23 Nov 2017 11:50:47 +0000 Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTP id 904bc182; Thu, 23 Nov 2017 11:45:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-type:content-transfer-encoding; s=mail; bh=Xi49jUpx0eoW mTO+pGQkRJok/S0=; b=GUP5J6ogsGdsqQJWct6ciNuj60affgIWw9eWRExsQpBk 0WQ9kTL0w7LMMdNOvrh70Ilouyw1+9QZvRz8tzpnV/pH6eSAICm+d8wQ2tmTUaM2 op2b5594S8ymtci6PTlrR2ySGHmMmt2f8IvFeRP4P5zNmbRoDHLlVHNqELg2C4uz Ny2WHsBQYIIO0nyNDq4zWPbCEzzlYo8UUO4V9oawI6+VxbsU1FcQ5o0UzuoVkSMu PZDCa36h55X87SADlSPdc2RtM2c6/NlYJuPV6tPZeIaLJ8mSpSO2E9b0FkBqknqZ 6g6GrXnZLfN3QZbCSlT/TziPPCWLWP8sLAl+Om14pA== Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id a7203257 (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Thu, 23 Nov 2017 11:45:09 +0000 (UTC) From: "Jason A. Donenfeld" To: nickc@redhat.com, binutils@sourceware.org, linux-arm-kernel@lists.infradead.org, linux@armlinux.org.uk, linux-kernel@vger.kernel.org Subject: [PATCH v2] arm: detect buggy binutils when in thumb2 mode Date: Thu, 23 Nov 2017 12:50:21 +0100 Message-Id: <20171123115021.25213-1-Jason@zx2c4.com> In-Reply-To: <20171123114802.24942-1-Jason@zx2c4.com> References: <20171123114802.24942-1-Jason@zx2c4.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171123_035045_688972_9773E8D7 X-CRM114-Status: GOOD ( 15.50 ) X-Spam-Score: -2.0 (--) X-Spam-Report: SpamAssassin version 3.4.1 on bombadil.infradead.org summary: Content analysis details: (-2.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Jason A. Donenfeld" , stable@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org List-Id: linux-imx-kernel.lists.patchwork.ozlabs.org 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 Cc: Russell King Cc: nickc@redhat.com Cc: stable@vger.kernel.org --- Had the file mode wrong on the submission from a second ago, sorry about that. arch/arm/Makefile | 7 +++++-- arch/arm/tools/Makefile | 5 ++++- arch/arm/tools/toolcheck | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100755 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 100755 index 000000000000..04fc44b750d2 --- /dev/null +++ b/arch/arm/tools/toolcheck @@ -0,0 +1,44 @@ +#!/bin/bash +# +# Copyright 2017 Jason A. Donenfeld . 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