From patchwork Fri Sep 21 15:56:00 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Chemparathy X-Patchwork-Id: 1492401 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 D6296400ED for ; Fri, 21 Sep 2012 16:02:56 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1TF5e5-0006pH-1x; Fri, 21 Sep 2012 16:00:01 +0000 Received: from bear.ext.ti.com ([192.94.94.41]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1TF5bM-0005ff-58 for linux-arm-kernel@lists.infradead.org; Fri, 21 Sep 2012 15:57:21 +0000 Received: from dlelxv30.itg.ti.com ([172.17.2.17]) by bear.ext.ti.com (8.13.7/8.13.7) with ESMTP id q8LFuaXC017712; Fri, 21 Sep 2012 10:56:36 -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 q8LFuaUc001186; Fri, 21 Sep 2012 10:56:36 -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, 21 Sep 2012 10:56:36 -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 q8LFuaMD006477; Fri, 21 Sep 2012 10:56:36 -0500 Received: from a0875269 by ares-ubuntu.am.dhcp.ti.com with local (Exim 4.76) (envelope-from ) id 1TF5am-00050E-8F; Fri, 21 Sep 2012 11:56:36 -0400 From: Cyril Chemparathy To: Subject: [PATCH v3 RESEND 02/17] ARM: add self test for runtime patch mechanism Date: Fri, 21 Sep 2012 11:56:00 -0400 Message-ID: <1348242975-19184-3-git-send-email-cyril@ti.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1348242975-19184-1-git-send-email-cyril@ti.com> References: <1348242975-19184-1-git-send-email-cyril@ti.com> MIME-Version: 1.0 X-Spam-Note: CRM114 invocation failed X-Spam-Score: -7.4 (-------) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-7.4 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high trust [192.94.94.41 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.5 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: catalin.marinas@arm.com, linus.walleij@linaro.org, will.deacon@arm.com, grant.likely@secretlab.ca, paul.gortmaker@windriver.com, vincent.guittot@linaro.org, nico@linaro.org, Cyril Chemparathy , davidb@codeaurora.org, plagnioj@jcrosoft.com, arnd@arndb.de, marc.zyngier@arm.com, rob.herring@calxeda.com, vitalya@ti.com, tglx@linutronix.de, linux-arm-kernel@lists.infradead.org, rmallon@gmail.com, frank.rowand@am.sony.com, sjg@chromium.org, sboyd@codeaurora.org, linux-kernel@vger.kernel.org, rabin@rab.in, hsweeten@visionengravers.com, tj@kernel.org 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 | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 98a3a1a..5bfaa20 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -205,6 +205,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 28a6367..0be9ef3 100644 --- a/arch/arm/kernel/runtime-patch.c +++ b/arch/arm/kernel/runtime-patch.c @@ -168,6 +168,78 @@ static int apply_patch_imm8(const struct patch_info *p) return 0; } +#ifdef CONFIG_ARM_RUNTIME_PATCH_TEST + +struct patch_test_imm8 { + u16 imm; + u16 shift; + u32 insn; +}; + +static void __init __used __naked __patch_test_code_imm8(void) +{ + __asm__ __volatile__ ( + + /* a single test case */ + " .macro test_one, imm, sft\n" + " .hword \\imm\n" + " .hword \\sft\n" + " add r1, r2, #(\\imm << \\sft)\n" + " .endm\n" + + /* a sequence of tests at 'inc' increments of shift */ + " .macro test_seq, imm, sft, max, inc\n" + " test_one \\imm, \\sft\n" + " .if \\sft < \\max\n" + " test_seq \\imm, (\\sft + \\inc), \\max, \\inc\n" + " .endif\n" + " .endm\n" + + /* an empty record to mark the end */ + " .macro test_end\n" + " .hword 0, 0\n" + " .word 0\n" + " .endm\n" + + /* finally generate the test sequences */ + " test_seq 0x41, 0, 24, 1\n" + " test_seq 0x81, 0, 24, 2\n" + " test_end\n" + : : : "r1", "r2", "cc"); +} + +static void __init test_patch_imm8(void) +{ + u32 test_code_addr = (u32)(&__patch_test_code_imm8); + struct patch_test_imm8 *test = (void *)(test_code_addr & ~1); + u32 ninsn, insn, patched_insn; + int i, err; + + insn = test[0].insn; + for (i = 0; test[i].insn; i++) { + err = do_patch_imm8(insn, test[i].imm << test[i].shift, &ninsn); + __patch_text(&patched_insn, ninsn); + + if (err) { + pr_err("rtpatch imm8: failed at imm %x, shift %d\n", + test[i].imm, test[i].shift); + } else if (patched_insn != test[i].insn) { + pr_err("rtpatch imm8: failed, need %x got %x\n", + test[i].insn, patched_insn); + } else { + pr_debug("rtpatch imm8: imm %x, shift %d, %x -> %x\n", + test[i].imm, test[i].shift, insn, + patched_insn); + } + } +} + +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); @@ -189,5 +261,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)); }