From patchwork Sat Aug 11 01:24:45 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Chemparathy X-Patchwork-Id: 1307531 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork1.kernel.org (Postfix) with ESMTP id 772B03FC66 for ; Sat, 11 Aug 2012 01:32:50 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1T00VY-00019j-Ue; Sat, 11 Aug 2012 01:28:53 +0000 Received: from devils.ext.ti.com ([198.47.26.153]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1T00SO-0007wU-SE for linux-arm-kernel@lists.infradead.org; Sat, 11 Aug 2012 01:25:52 +0000 Received: from dlelxv30.itg.ti.com ([172.17.2.17]) by devils.ext.ti.com (8.13.7/8.13.7) with ESMTP id q7B1PNhI014206; Fri, 10 Aug 2012 20:25:23 -0500 Received: from DFLE72.ent.ti.com (dfle72.ent.ti.com [128.247.5.109]) by dlelxv30.itg.ti.com (8.13.8/8.13.8) with ESMTP id q7B1PNZY010278; Fri, 10 Aug 2012 20:25:23 -0500 Received: from dlelxv22.itg.ti.com (172.17.1.197) by dfle72.ent.ti.com (128.247.5.109) with Microsoft SMTP Server id 14.1.323.3; Fri, 10 Aug 2012 20:25:22 -0500 Received: from ares-ubuntu.am.dhcp.ti.com (ares-ubuntu.am.dhcp.ti.com [158.218.103.17]) by dlelxv22.itg.ti.com (8.13.8/8.13.8) with ESMTP id q7B1PMpm001366; Fri, 10 Aug 2012 20:25:22 -0500 Received: from a0875269 by ares-ubuntu.am.dhcp.ti.com with local (Exim 4.76) (envelope-from ) id 1T00SA-00044Z-Il; Fri, 10 Aug 2012 21:25:22 -0400 From: Cyril Chemparathy To: , Subject: [PATCH v2 02/22] ARM: add self test for runtime patch mechanism Date: Fri, 10 Aug 2012 21:24:45 -0400 Message-ID: <1344648306-15619-3-git-send-email-cyril@ti.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1344648306-15619-1-git-send-email-cyril@ti.com> References: <1344648306-15619-1-git-send-email-cyril@ti.com> MIME-Version: 1.0 X-Spam-Note: CRM114 invocation failed X-Spam-Score: -6.9 (------) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-6.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high trust [198.47.26.153 listed in list.dnswl.org] -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] Cc: linux@arm.linux.org.uk, arnd@arndb.de, nico@linaro.org, catalin.marinas@arm.com, will.deacon@arm.com, grant.likely@secretlab.ca, Cyril Chemparathy X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org This patch adds basic sanity tests to ensure that the instruction patching results in valid instruction encodings. This is done by verifying the output of the patch process against a vector of assembler generated instructions at init time. Signed-off-by: Cyril Chemparathy --- arch/arm/Kconfig | 12 ++++++++++++ arch/arm/kernel/runtime-patch.c | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index d0a04ad..7e552dc 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -211,6 +211,18 @@ config ARM_PATCH_PHYS_VIRT this feature (eg, building a kernel for a single machine) and you need to shrink the kernel to the minimal size. +config ARM_RUNTIME_PATCH_TEST + bool "Self test runtime patching mechanism" if ARM_RUNTIME_PATCH + default y + help + Select this to enable init time self checking for the runtime kernel + patching mechanism. This enables an ISA specific set of tests that + ensure that the instructions generated by the patch process are + consistent with those generated by the assembler at compile time. + + Only disable this option if you need to shrink the kernel to the + minimal size. + config NEED_MACH_IO_H bool help diff --git a/arch/arm/kernel/runtime-patch.c b/arch/arm/kernel/runtime-patch.c index fd37a2b..c471d8c 100644 --- a/arch/arm/kernel/runtime-patch.c +++ b/arch/arm/kernel/runtime-patch.c @@ -163,6 +163,44 @@ static int apply_patch_imm8(const struct patch_info *p) return 0; } +#ifdef CONFIG_ARM_RUNTIME_PATCH_TEST +static void __init __used __naked __patch_test_code_imm8(void) +{ + __asm__ __volatile__ ( + " .irp shift1, 0, 6, 12, 18\n" + " .irp shift2, 0, 1, 2, 3, 4, 5\n" + " add r1, r2, #(0x41 << (\\shift1 + \\shift2))\n" + " .endr\n" + " .endr\n" + " .word 0\n" + : : : + ); +} + +static void __init test_patch_imm8(void) +{ + u32 test_code_addr = (u32)(&__patch_test_code_imm8); + u32 *test_code = (u32 *)(test_code_addr & ~0x3); + int i, ret; + u32 ninsn, insn; + + insn = test_code[0]; + for (i = 0; test_code[i]; i++) { + ret = do_patch_imm8(insn, 0x41 << i, &ninsn); + if (ret < 0) + pr_err("runtime patch (imm8): failed at shift %d\n", i); + else if (ninsn != test_code[i]) + pr_err("runtime patch (imm8): failed, need %x got %x\n", + test_code[i], ninsn); + } +} + +static void __init runtime_patch_test(void) +{ + test_patch_imm8(); +} +#endif + int runtime_patch(const void *table, unsigned size) { const struct patch_info *p = table, *end = (table + size); @@ -185,5 +223,8 @@ void __init runtime_patch_kernel(void) const void *start = &__runtime_patch_table_begin; const void *end = &__runtime_patch_table_end; +#ifdef CONFIG_ARM_RUNTIME_PATCH_TEST + runtime_patch_test(); +#endif BUG_ON(runtime_patch(start, end - start)); }