From patchwork Tue Jan 26 17:10:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 60483 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp2088949lbb; Tue, 26 Jan 2016 09:11:54 -0800 (PST) X-Received: by 10.98.86.214 with SMTP id h83mr35248186pfj.112.1453828312595; Tue, 26 Jan 2016 09:11:52 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id wb3si3082437pab.114.2016.01.26.09.11.52; Tue, 26 Jan 2016 09:11:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dkim=pass header.i=@linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S967062AbcAZRLg (ORCPT + 30 others); Tue, 26 Jan 2016 12:11:36 -0500 Received: from mail-wm0-f53.google.com ([74.125.82.53]:34183 "EHLO mail-wm0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S967018AbcAZRLW (ORCPT ); Tue, 26 Jan 2016 12:11:22 -0500 Received: by mail-wm0-f53.google.com with SMTP id u188so115330122wmu.1 for ; Tue, 26 Jan 2016 09:11:21 -0800 (PST) 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=yR5Xnd8j37tGEeCYqflsSJCJeL9KBQq3IVxY/PTb5gs=; b=d9XZkdImZAIqKsx00P5rSl2xMZDZCVLbrxNBFktiABIRBZPX6BH8UIDTkDOIv8nQaU fupLuNK18fO5IWMtfMb42A97K1vCUiVvJKkhRN4ddosoVQRTJB+CaVB5dbtRGJ14C9BX kMXlkSa0V5yDPRAYJZpd2yaRKUUyMVKDhPBA4= 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=yR5Xnd8j37tGEeCYqflsSJCJeL9KBQq3IVxY/PTb5gs=; b=cqz9Bi5JHmF0KwcbER4Ce/XIuu8jHIp3HbY/XfIjd8Xonxa4YFHhfgJAaOlu0o/gZe jZYfshSXIB3PqqdAGm7+7CYcMpdadlzIy726tW4AF9/Pr4kONzr/po1EKMGMAl6VmQaG gO+sPUVrxwLqdfLANs2+4/aEOAk5M66GAj6z4Vla4yZdb3xaFsOtP6iHpeLdIrz2qcuJ Vqu/r+sSOsS9XM/EGm6kGj9MC5s3Lxh2TOTSEDr8PeMo0WLPrp8Kcv3Ywxg/ApfPwU7f OBYoJtZ7CJRN7taLW52/diUktlsEgIn6aB3aragHx8KynG9A5r9CITvG9O6f2Z1Nzb1v +GYw== X-Gm-Message-State: AG10YOQZ+jgLUxgHnTFIrJo8Oi9fwlzbmHshlQV4vpI2VBe9HxP7BRmHz1/xLB6PnaQ7sWg0 X-Received: by 10.194.93.102 with SMTP id ct6mr28626961wjb.157.1453828281071; Tue, 26 Jan 2016 09:11:21 -0800 (PST) Received: from localhost.localdomain ([195.55.142.58]) by smtp.gmail.com with ESMTPSA id ko2sm2328617wjc.9.2016.01.26.09.11.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jan 2016 09:11:20 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, kernel-hardening@lists.openwall.com, will.deacon@arm.com, catalin.marinas@arm.com, mark.rutland@arm.com, leif.lindholm@linaro.org, keescook@chromium.org, linux-kernel@vger.kernel.org Cc: stuart.yoder@freescale.com, bhupesh.sharma@freescale.com, arnd@arndb.de, marc.zyngier@arm.com, christoffer.dall@linaro.org, labbott@fedoraproject.org, matt@codeblueprint.co.uk, Ard Biesheuvel Subject: [PATCH v4 09/22] extable: add support for relative extables to search and sort routines Date: Tue, 26 Jan 2016 18:10:36 +0100 Message-Id: <1453828249-14467-10-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1453828249-14467-1-git-send-email-ard.biesheuvel@linaro.org> References: <1453828249-14467-1-git-send-email-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds support to the generic search_extable() and sort_extable() implementations for dealing with exception table entries whose fields contain relative offsets rather than absolute addresses. Acked-by: Helge Deller Acked-by: Heiko Carstens Acked-by: H. Peter Anvin Acked-by: Tony Luck Acked-by: Will Deacon Signed-off-by: Ard Biesheuvel --- lib/extable.c | 50 ++++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) -- 2.5.0 diff --git a/lib/extable.c b/lib/extable.c index 4cac81ec225e..0be02ad561e9 100644 --- a/lib/extable.c +++ b/lib/extable.c @@ -14,7 +14,37 @@ #include #include +#ifndef ARCH_HAS_RELATIVE_EXTABLE +#define ex_to_insn(x) ((x)->insn) +#else +static inline unsigned long ex_to_insn(const struct exception_table_entry *x) +{ + return (unsigned long)&x->insn + x->insn; +} +#endif + #ifndef ARCH_HAS_SORT_EXTABLE +#ifndef ARCH_HAS_RELATIVE_EXTABLE +#define swap_ex NULL +#else +static void swap_ex(void *a, void *b, int size) +{ + struct exception_table_entry *x = a, *y = b, tmp; + int delta = b - a; + + tmp = *x; + x->insn = y->insn + delta; + y->insn = tmp.insn - delta; + +#ifdef swap_ex_entry_fixup + swap_ex_entry_fixup(x, y, tmp, delta); +#else + x->fixup = y->fixup + delta; + y->fixup = tmp.fixup - delta; +#endif +} +#endif /* ARCH_HAS_RELATIVE_EXTABLE */ + /* * The exception table needs to be sorted so that the binary * search that we use to find entries in it works properly. @@ -26,9 +56,9 @@ static int cmp_ex(const void *a, const void *b) const struct exception_table_entry *x = a, *y = b; /* avoid overflow */ - if (x->insn > y->insn) + if (ex_to_insn(x) > ex_to_insn(y)) return 1; - if (x->insn < y->insn) + if (ex_to_insn(x) < ex_to_insn(y)) return -1; return 0; } @@ -37,7 +67,7 @@ void sort_extable(struct exception_table_entry *start, struct exception_table_entry *finish) { sort(start, finish - start, sizeof(struct exception_table_entry), - cmp_ex, NULL); + cmp_ex, swap_ex); } #ifdef CONFIG_MODULES @@ -48,13 +78,15 @@ void sort_extable(struct exception_table_entry *start, void trim_init_extable(struct module *m) { /*trim the beginning*/ - while (m->num_exentries && within_module_init(m->extable[0].insn, m)) { + while (m->num_exentries && + within_module_init(ex_to_insn(&m->extable[0]), m)) { m->extable++; m->num_exentries--; } /*trim the end*/ while (m->num_exentries && - within_module_init(m->extable[m->num_exentries-1].insn, m)) + within_module_init(ex_to_insn(&m->extable[m->num_exentries - 1]), + m)) m->num_exentries--; } #endif /* CONFIG_MODULES */ @@ -81,13 +113,13 @@ search_extable(const struct exception_table_entry *first, * careful, the distance between value and insn * can be larger than MAX_LONG: */ - if (mid->insn < value) + if (ex_to_insn(mid) < value) first = mid + 1; - else if (mid->insn > value) + else if (ex_to_insn(mid) > value) last = mid - 1; else return mid; - } - return NULL; + } + return NULL; } #endif