From patchwork Sat Aug 20 07:45:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 74359 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp659521qga; Sat, 20 Aug 2016 00:48:00 -0700 (PDT) X-Received: by 10.98.157.12 with SMTP id i12mr22123971pfd.164.1471679280162; Sat, 20 Aug 2016 00:48:00 -0700 (PDT) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id b185si9544068pfa.125.2016.08.20.00.48.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Aug 2016 00:48:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bb0zE-0004BS-JP; Sat, 20 Aug 2016 07:46:36 +0000 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bb0yi-00045G-QJ for linux-arm-kernel@lists.infradead.org; Sat, 20 Aug 2016 07:46:07 +0000 Received: by mail-wm0-x229.google.com with SMTP id i5so70663311wmg.0 for ; Sat, 20 Aug 2016 00:45:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2cFN3+DfOb+4IB/SFGp4gjN5HAbaRfvnLg9mWd3hL2o=; b=CKVMZ88j8pBlKeMPr497FjACJkHYugX2MrEPiqas69j0VkqIcDfqPo39XhTfE9EjZq 9UW8BuSrWU2Bq02JdUVVqRa2FS8zYrQF+1FzjPK9yBQvT+xjMJxGjswt73OHofxgY/D3 oRlfY3J1yNxWWuVXZQz0qZ86PquoZs86g/sIk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2cFN3+DfOb+4IB/SFGp4gjN5HAbaRfvnLg9mWd3hL2o=; b=AZGvsr4tL3dHzusVuLN8u21UisjaxtN6nFNyWZpjQLbyvr7Uir+D8y7B1FxYKPsjEz TX5WsCVSGaQJ3TjlQCGzU9BH5307BapJ2Je25MfCzLgxSLKYgDKP68OgF6k72d99nvD2 MVVxdlgu0ODtfRMFUJthBykS1tpSxx6y9Iu/aWmc5Jgo60xvBjCmcKfvP3nMkTg3wQ0t SbaZ9YDpzzdU8K2gCZ6+aVzVTQeilk3mT1HKRqct990lG6SDytTVPdex9IQ3lSDjAt3R U8Gu3yvA8OS41etvr6Wr2Ue76a3nVj9o8beeCbbppX0PmmvBHmT+eCmsSNEAN4wdctPN BZVA== X-Gm-Message-State: AEkooutUFYPv0HKaZHVbQUTG8w7BrMcmv5YFpo52Ld3lQ6pZUaeliRb3krCTp7SNNoBw6b8L X-Received: by 10.194.148.202 with SMTP id tu10mr10225479wjb.56.1471679142584; Sat, 20 Aug 2016 00:45:42 -0700 (PDT) Received: from localhost.localdomain (46.red-81-37-107.dynamicip.rima-tde.net. [81.37.107.46]) by smtp.gmail.com with ESMTPSA id va3sm10619944wjb.18.2016.08.20.00.45.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 20 Aug 2016 00:45:41 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux@arm.linux.org.uk, dave.martin@arm.com Subject: [PATCH 2/2] ARM: add interworking support to Thumb-2 kernel Date: Sat, 20 Aug 2016 09:45:31 +0200 Message-Id: <1471679131-3570-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1471679131-3570-1-git-send-email-ard.biesheuvel@linaro.org> References: <1471679131-3570-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160820_004605_252678_9EAE3D83 X-CRM114-Status: GOOD ( 22.51 ) X-Spam-Score: -2.0 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -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_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org ARM/Thumb interworking is currently not allowed in modules at all: external module dependencies can only be fulfilled by symbols of the same flavour, but even inside a module, jump and call relocations between objects are rejected if they would incur a mode switch. This patch relaxes that restriction, by allowing function calls ('bl') from T32 into A32 code, and vice versa, by fixing up the bl instruction to a blx instruction, with the appropriate rounding applied to the offset. For jump ('b') instructions, this is not possible (the ISA does not provide jump instructions that switch mode) and so a PLT entry emitted instead, which is inherently interworking-aware since it uses a 'ldr pc, =xxx' instruction. Since this requires module PLT support, add the 'select ARM_MODULE_PLTS ' to config THUMB2_KERNEL. Signed-off-by: Ard Biesheuvel --- arch/arm/Kconfig | 1 + arch/arm/include/asm/module.h | 3 +- arch/arm/kernel/module-plts.c | 21 +++++--- arch/arm/kernel/module.c | 55 +++++++++++++++----- 4 files changed, 61 insertions(+), 19 deletions(-) -- 2.7.4 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index a9c4e48bb7ec..c786337f3c8b 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1544,6 +1544,7 @@ config THUMB2_KERNEL select AEABI select ARM_ASM_UNIFIED select ARM_UNWIND + select ARM_MODULE_PLTS help By enabling this option, the kernel will be compiled in Thumb-2 mode. A compiler/assembler that understand the unified diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h index 464748b9fd7d..82ba2a82f9a3 100644 --- a/arch/arm/include/asm/module.h +++ b/arch/arm/include/asm/module.h @@ -28,7 +28,8 @@ struct mod_arch_specific { #endif }; -u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val); +u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val, + bool from_thumb); /* * Add the ARM architecture version to the version magic string diff --git a/arch/arm/kernel/module-plts.c b/arch/arm/kernel/module-plts.c index a910f1db0c14..c239bc92f81b 100644 --- a/arch/arm/kernel/module-plts.c +++ b/arch/arm/kernel/module-plts.c @@ -18,12 +18,15 @@ #define PLT_ENT_COUNT (PLT_ENT_STRIDE / sizeof(u32)) #define PLT_ENT_SIZE (sizeof(struct plt_entries) / PLT_ENT_COUNT) -#ifdef CONFIG_THUMB2_KERNEL -#define PLT_ENT_LDR __opcode_to_mem_thumb32(0xf8dff000 | \ +#define PLT_ENT_LDR_ARM __opcode_to_mem_arm(0xe59ff000 | \ + (PLT_ENT_STRIDE - 8)) +#define PLT_ENT_LDR_THUMB __opcode_to_mem_thumb32(0xf8dff000 | \ (PLT_ENT_STRIDE - 4)) + +#ifdef CONFIG_THUMB2_KERNEL +#define PLT_ENT_LDR PLT_ENT_LDR_THUMB #else -#define PLT_ENT_LDR __opcode_to_mem_arm(0xe59ff000 | \ - (PLT_ENT_STRIDE - 8)) +#define PLT_ENT_LDR PLT_ENT_LDR_ARM #endif struct plt_entries { @@ -31,7 +34,8 @@ struct plt_entries { u32 lit[PLT_ENT_COUNT]; }; -u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val) +u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val, + bool from_thumb) { struct plt_entries *plt = (struct plt_entries *)mod->arch.plt->sh_addr; int idx = 0; @@ -45,7 +49,10 @@ u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val) plt += (mod->arch.plt_count - 1) / PLT_ENT_COUNT; idx = (mod->arch.plt_count - 1) % PLT_ENT_COUNT; - if (plt->lit[idx] == val) + if (plt->lit[idx] == val && + (!IS_ENABLED(CONFIG_THUMB2_KERNEL) || + plt->ldr[idx] == from_thumb ? PLT_ENT_LDR_THUMB : + PLT_ENT_LDR_ARM)) return (u32)&plt->ldr[idx]; idx = (idx + 1) % PLT_ENT_COUNT; @@ -65,6 +72,8 @@ u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val) else plt->lit[idx] = val; + if (IS_ENABLED(CONFIG_THUMB2_KERNEL) && !from_thumb) + plt->ldr[idx] = PLT_ENT_LDR_ARM; return (u32)&plt->ldr[idx]; } diff --git a/arch/arm/kernel/module.c b/arch/arm/kernel/module.c index 6c22b13cbd12..f6c4059d0006 100644 --- a/arch/arm/kernel/module.c +++ b/arch/arm/kernel/module.c @@ -103,7 +103,8 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, case R_ARM_PC24: case R_ARM_CALL: case R_ARM_JUMP24: - if (sym->st_value & 3) { + if (!IS_ENABLED(CONFIG_THUMB2_KERNEL) && + sym->st_value & 3) { pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (ARM -> Thumb)\n", module->name, relindex, i, symname); return -ENOEXEC; @@ -121,12 +122,17 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, * supported range. Note that 'offset + loc + 8' * contains the absolute jump target, i.e., * @sym + addend, corrected for the +8 PC bias. + * Also emit PLT entries for interworking jumps, + * and for conditional interworking call instructions. */ if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) && (offset <= (s32)0xfe000000 || - offset >= (s32)0x02000000)) + offset >= (s32)0x02000000 || + ((sym->st_value & 1) != 0 && + (ELF32_R_TYPE(rel->r_info) != R_ARM_CALL || + __mem_to_opcode_arm(*(u32 *)loc) >> 28 != 0xe)))) offset = get_module_plt(module, loc, - offset + loc + 8) + offset + loc + 8, false) - loc - 8; if (offset <= (s32)0xfe000000 || @@ -138,11 +144,19 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, return -ENOEXEC; } + if (offset & 1) { + /* convert bl instruction to blx */ + tmp = 0xf0000000 | (offset & 2) << 23; + *(u32 *)loc &= __opcode_to_mem_arm(~BIT(24)); + *(u32 *)loc |= __opcode_to_mem_arm(tmp); + } + offset >>= 2; offset &= 0x00ffffff; *(u32 *)loc &= __opcode_to_mem_arm(0xff000000); *(u32 *)loc |= __opcode_to_mem_arm(offset); + break; case R_ARM_V4BX: @@ -180,8 +194,9 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, case R_ARM_THM_CALL: case R_ARM_THM_JUMP24: /* - * For function symbols, only Thumb addresses are - * allowed (no interworking). + * For function symbols, we need to force a + * Thumb -> ARM mode switch if the destination + * address has its Thumb bit (bit 0) cleared. * This applies equally to untyped symbols that * resolve to external ksyms: EXPORT_SYMBOL() * strips the function annotation, but we can @@ -197,9 +212,9 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, (ELF32_ST_TYPE(sym->st_info) == STT_NOTYPE && sym->st_shndx == SHN_UNDEF)) && !(sym->st_value & 1)) { - pr_err("%s: section %u reloc %u sym '%s': unsupported interworking call (Thumb -> ARM)\n", - module->name, relindex, i, symname); - return -ENOEXEC; + tmp = sym->st_value; + } else { + tmp = sym->st_value | 1; } upper = __mem_to_opcode_thumb16(*(u16 *)loc); @@ -227,18 +242,34 @@ apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, ((lower & 0x07ff) << 1); if (offset & 0x01000000) offset -= 0x02000000; - offset += sym->st_value - loc; + offset += tmp; + + /* + * When fixing up a bl instruction to blx, the address + * of the call site must be rounded down in the + * calculation of 'offset'. As this could potentially + * cause 'offset' to go out of range, we need to do + * this before performing the range check. + */ + tmp = offset & 1 ? loc : loc & ~2; + offset -= tmp; /* * Route through a PLT entry if 'offset' exceeds the - * supported range. + * supported range. Also emit PLT entries for + * interworking jump instructions. */ if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) && (offset <= (s32)0xff000000 || - offset >= (s32)0x01000000)) + offset >= (s32)0x01000000 || + (ELF32_R_TYPE(rel->r_info) != R_ARM_THM_CALL && + !(sym->st_value & 1)))) offset = get_module_plt(module, loc, - offset + loc + 4) + offset + tmp + 4, true) - loc - 4; + else if (!(offset & 1)) + /* fix up bl -> blx */ + lower &= ~(1 << 12); if (offset <= (s32)0xff000000 || offset >= (s32)0x01000000) {