From patchwork Wed Jan 27 04:12:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinyang He X-Patchwork-Id: 12048883 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BEE5CC433E0 for ; Wed, 27 Jan 2021 04:27:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 70C3520715 for ; Wed, 27 Jan 2021 04:27:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238665AbhA0E1j (ORCPT ); Tue, 26 Jan 2021 23:27:39 -0500 Received: from mail.loongson.cn ([114.242.206.163]:41940 "EHLO loongson.cn" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S238353AbhA0ENh (ORCPT ); Tue, 26 Jan 2021 23:13:37 -0500 Received: from loongson.localdomain (unknown [113.200.148.30]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9DxS+Qp6BBg750NAA--.21121S2; Wed, 27 Jan 2021 12:12:25 +0800 (CST) From: Jinyang He To: Thomas Bogendoerfer Cc: Huacai Chen , Jiaxun Yang , linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] MIPS: relocatable: Provide kaslr_offset() to get the kernel offset Date: Wed, 27 Jan 2021 12:12:25 +0800 Message-Id: <1611720745-8256-1-git-send-email-hejinyang@loongson.cn> X-Mailer: git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a7IUYj9aPUUUUU== X-CM-SenderInfo: pkhmx0p1dqwqxorr0wxvrqhubq/ Precedence: bulk List-ID: X-Mailing-List: linux-mips@vger.kernel.org Use kimage_vaddr to indicate kernel start address. Provide kaslr_offset() to get the kernel offset when KASLR is enabled. Error may occur before update_kimage_vaddr(), so put it at the end of the offset branch. Fixes: a307a4ce9ecd ("MIPS: Loongson64: Add KASLR support") Reported-by: kernel test robot Signed-off-by: Jinyang He --- arch/mips/include/asm/page.h | 6 ++++++ arch/mips/kernel/relocate.c | 12 ++++++++++++ arch/mips/kernel/setup.c | 3 +++ 3 files changed, 21 insertions(+) diff --git a/arch/mips/include/asm/page.h b/arch/mips/include/asm/page.h index 6a77bc4..9429520 100644 --- a/arch/mips/include/asm/page.h +++ b/arch/mips/include/asm/page.h @@ -255,6 +255,12 @@ extern bool __virt_addr_valid(const volatile void *kaddr); #define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_TSK_EXEC +extern unsigned long kimage_vaddr; +static inline unsigned long kaslr_offset(void) +{ + return kimage_vaddr - VMLINUX_LOAD_ADDRESS; +} + #include #include diff --git a/arch/mips/kernel/relocate.c b/arch/mips/kernel/relocate.c index c643c81..2cbc3b1 100644 --- a/arch/mips/kernel/relocate.c +++ b/arch/mips/kernel/relocate.c @@ -300,6 +300,15 @@ static inline int __init relocation_addr_valid(void *loc_new) return 1; } +static inline void __init update_kimage_vaddr(unsigned long value, + unsigned long *addr, long offset) +{ + unsigned long new_value = value + offset; + unsigned long *new_addr = (void *)addr + offset; + + *new_addr = new_value; +} + #if defined(CONFIG_USE_OF) void __weak *plat_get_fdt(void) { @@ -410,6 +419,9 @@ void *__init relocate_kernel(void) /* Return the new kernel's entry point */ kernel_entry = RELOCATED(start_kernel); + + /* Error may occur before, so keep it at last */ + update_kimage_vaddr(kimage_vaddr, &kimage_vaddr, offset); } out: return kernel_entry; diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c index 7e1f8e2..ad37b24 100644 --- a/arch/mips/kernel/setup.c +++ b/arch/mips/kernel/setup.c @@ -84,6 +84,9 @@ static struct resource code_resource = { .name = "Kernel code", }; static struct resource data_resource = { .name = "Kernel data", }; static struct resource bss_resource = { .name = "Kernel bss", }; +unsigned long kimage_vaddr __initdata = VMLINUX_LOAD_ADDRESS; +EXPORT_SYMBOL(kimage_vaddr); + static void *detect_magic __initdata = detect_memory_region; #ifdef CONFIG_MIPS_AUTO_PFN_OFFSET