Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3559438pxp; Tue, 8 Mar 2022 17:27:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJyS36DJVp2tkzLGHhtIDjcqzQYxPSxE6sIMyCQ3re+0kqI21jC6mWVHjTNz3OPROtmkZjzh X-Received: by 2002:a17:902:714b:b0:151:d8fa:fd98 with SMTP id u11-20020a170902714b00b00151d8fafd98mr17838626plm.146.1646789230256; Tue, 08 Mar 2022 17:27:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646789230; cv=none; d=google.com; s=arc-20160816; b=uf1cIlBh6JANUpCrLBhG1sPwV4g938H3xDJWzNgB3TEIjKZotnzvpSx2lT5wueldZu KFFzB/S/DLq8LAixHSA5dKZxKr0zOXi/g/lUfPrJ1klfp9DEsqXAU67MJpJxwkhnfDnW SAncxipqj/H+XBMCu7nkh1QKsjNDN0azohnpcKOfWfu0zq6uBf5Lr+OBH7Ceug4SvP1E eHGS8oE8xB3T3Z402JkL0cep2N5sx2XNg3P8EPukMbRSA5vjUBozRAf2Yi+QEQ70suIu WZ/nN0SOyr9xRmU+Zq0Xs2xAHfNvxLxQJonLrij9UIreBGyAGgouN1RJRpBBFn8QBCMA 9T0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=HNYTszssgG35WY39ZthzmE1EwZCbPhmoekt0Di38IEs=; b=bKgnen39Vs+uH1M4Mpyn5PkAGYQNugzNbohQPNJjJZhhqj7kBvFjz1PNmnQRAJB5Ow LstUSv6xpIUVSsEJKWaN549RJWJu20mJ+VIy6pbfCtCL2NKE8E7iQ8kO/9vIs0NBVnBw gyw8R1Q2Eu4NSqFwF5QcjR6rvM7sPBUQer9lX5C9X2bcjDjdejcPPT4s5a12Vwg+bRYW lImXUPDzjj1SZ8GzR/lMXRHqhdbN4K6usj6x4UqNp0T9+RvxPEpRWqW2rj4uYG1cOdVb dedTZxMlT0cqFsxoGulzhONEiBhqZpIkfJAtUmdBj19vRs/140jknLUgWBJIOq4UW8/j yCzA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id kb4-20020a17090ae7c400b001bd14e030b0si4360924pjb.136.2022.03.08.17.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Mar 2022 17:27:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E6081AE6C; Tue, 8 Mar 2022 16:16:28 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245448AbiCGUyr (ORCPT + 99 others); Mon, 7 Mar 2022 15:54:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245394AbiCGUyT (ORCPT ); Mon, 7 Mar 2022 15:54:19 -0500 Received: from gloria.sntech.de (gloria.sntech.de [185.11.138.130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F282289B8 for ; Mon, 7 Mar 2022 12:53:24 -0800 (PST) Received: from ip5b412258.dynamic.kabel-deutschland.de ([91.65.34.88] helo=phil.lan) by gloria.sntech.de with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nRKM1-0002DJ-Ar; Mon, 07 Mar 2022 21:53:17 +0100 From: Heiko Stuebner To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, wefu@redhat.com, liush@allwinnertech.com, guoren@kernel.org, atishp@atishpatra.org, anup@brainfault.org, drew@beagleboard.org, hch@lst.de, arnd@arndb.de, wens@csie.org, maxime@cerno.tech, gfavor@ventanamicro.com, andrea.mondelli@huawei.com, behrensj@mit.edu, xinhaoqu@huawei.com, mick@ics.forth.gr, allen.baum@esperantotech.com, jscheid@ventanamicro.com, rtrauben@gmail.com, samuel@sholland.org, cmuellner@linux.com, philipp.tomsich@vrull.eu, Heiko Stuebner Subject: [PATCH v7 04/13] riscv: implement module alternatives Date: Mon, 7 Mar 2022 21:53:01 +0100 Message-Id: <20220307205310.1905628-5-heiko@sntech.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220307205310.1905628-1-heiko@sntech.de> References: <20220307205310.1905628-1-heiko@sntech.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This allows alternatives to also be applied when loading modules and follows the implementation of other architectures (e.g. arm64). Signed-off-by: Heiko Stuebner --- arch/riscv/errata/sifive/errata.c | 7 ++++--- arch/riscv/include/asm/alternative.h | 3 ++- arch/riscv/kernel/alternative.c | 18 +++++++++++++---- arch/riscv/kernel/module.c | 29 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/arch/riscv/errata/sifive/errata.c b/arch/riscv/errata/sifive/errata.c index 4fe03ac41fd7..42457efcaaa3 100644 --- a/arch/riscv/errata/sifive/errata.c +++ b/arch/riscv/errata/sifive/errata.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -54,7 +55,7 @@ static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER] = { }, }; -static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid) +static u32 __init_or_module sifive_errata_probe(unsigned long archid, unsigned long impid) { int idx; u32 cpu_req_errata = 0; @@ -66,7 +67,7 @@ static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid) return cpu_req_errata; } -static void __init warn_miss_errata(u32 miss_errata) +static void __init_or_module warn_miss_errata(u32 miss_errata) { int i; @@ -79,7 +80,7 @@ static void __init warn_miss_errata(u32 miss_errata) pr_warn("----------------------------------------------------------------\n"); } -void __init sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, +void __init_or_module sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, unsigned long archid, unsigned long impid, unsigned int stage) { diff --git a/arch/riscv/include/asm/alternative.h b/arch/riscv/include/asm/alternative.h index 811bdd8027db..f0657b1b3174 100644 --- a/arch/riscv/include/asm/alternative.h +++ b/arch/riscv/include/asm/alternative.h @@ -18,8 +18,10 @@ #include #define RISCV_ALTERNATIVES_BOOT 0 /* alternatives applied during regular boot */ +#define RISCV_ALTERNATIVES_MODULE 1 /* alternatives applied during module-init */ void __init apply_boot_alternatives(void); +void apply_module_alternatives(void *start, size_t length); struct alt_entry { void *old_ptr; /* address of original instruciton or data */ @@ -37,6 +39,5 @@ struct errata_checkfunc_id { void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end, unsigned long archid, unsigned long impid, unsigned int stage); - #endif #endif diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternative.c index 02db62f55bac..223770b3945c 100644 --- a/arch/riscv/kernel/alternative.c +++ b/arch/riscv/kernel/alternative.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -23,7 +24,7 @@ static struct cpu_manufacturer_info_t { static void (*vendor_patch_func)(struct alt_entry *begin, struct alt_entry *end, unsigned long archid, unsigned long impid, - unsigned int stage) __initdata; + unsigned int stage) __initdata_or_module; static inline void __init riscv_fill_cpu_mfr_info(void) { @@ -58,9 +59,9 @@ static void __init init_alternative(void) * a feature detect on the boot CPU). No need to worry about other CPUs * here. */ -static void __init _apply_alternatives(struct alt_entry *begin, - struct alt_entry *end, - unsigned int stage) +static void __init_or_module _apply_alternatives(struct alt_entry *begin, + struct alt_entry *end, + unsigned int stage) { if (!vendor_patch_func) return; @@ -81,3 +82,12 @@ void __init apply_boot_alternatives(void) (struct alt_entry *)__alt_end, RISCV_ALTERNATIVES_BOOT); } + +#ifdef CONFIG_MODULES +void apply_module_alternatives(void *start, size_t length) +{ + _apply_alternatives((struct alt_entry *)start, + (struct alt_entry *)(start + length), + RISCV_ALTERNATIVES_MODULE); +} +#endif diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c index 68a9e3d1fe16..a778abd5b8b9 100644 --- a/arch/riscv/kernel/module.c +++ b/arch/riscv/kernel/module.c @@ -11,6 +11,7 @@ #include #include #include +#include #include static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v) @@ -416,3 +417,31 @@ void *module_alloc(unsigned long size) __builtin_return_address(0)); } #endif + +static const Elf_Shdr *find_section(const Elf_Ehdr *hdr, + const Elf_Shdr *sechdrs, + const char *name) +{ + const Elf_Shdr *s, *se; + const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; + + for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) { + if (strcmp(name, secstrs + s->sh_name) == 0) + return s; + } + + return NULL; +} + +int module_finalize(const Elf_Ehdr *hdr, + const Elf_Shdr *sechdrs, + struct module *me) +{ + const Elf_Shdr *s; + + s = find_section(hdr, sechdrs, ".alternative"); + if (s) + apply_module_alternatives((void *)s->sh_addr, s->sh_size); + + return 0; +} -- 2.30.2