From patchwork Tue Aug 15 10:48:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Song Shuai X-Patchwork-Id: 13353723 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D20CDC001B0 for ; Tue, 15 Aug 2023 10:50:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=a5VfL+MYtajgH024uQ9rAv4KgBYiHDYnb8BvmicgbXE=; b=V93ltWHDRXEwBF nc15mACvVG/MTKv3dLD+3iy6zqHdOFy4xVE7ZCKsQAc1GYpql/7BM6vgSGnQx6Yzb5oIwMnYHLkhP REe/c1v0DcVdTYLi1K8Z5Fi5EYnnQO7wSf3n1ZmU5iU1ZqlYq9azU1W5Cgsd8BIf6PaJeGIr/M9Nj W4RKMRtoNbbxRKCFxl4lmYDvGA93xZUkxxWrQEWjSGShVQ0SQ4SC53jPYQGZ9QiYyFNGHFDp2+Py2 GMoIzQD6oGwtdfHFnXGRvMXcYfI2dj6utBEPnGIkxKkcc9segwAyqnVcUEavOo2J/50JZ/K8Otk/O dW21J9zdWH++Xqos8FOA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qVrdF-001KNh-12; Tue, 15 Aug 2023 10:50:37 +0000 Received: from bg4.exmail.qq.com ([43.155.67.158]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qVrdA-001KKd-2A; Tue, 15 Aug 2023 10:50:35 +0000 X-QQ-mid: bizesmtp78t1692096533tqw77zvr Received: from localhost.localdomain ( [221.226.144.218]) by bizesmtp.qq.com (ESMTP) with id ; Tue, 15 Aug 2023 18:48:51 +0800 (CST) X-QQ-SSF: 01200000000000B0B000000A0000000 X-QQ-FEAT: rZJGTgY0+YMW5DdpUqE/uxFUSfrDxy1/9tAZByanbxvWWIqR870ykuNueNqb6 LIDON3wk2mEk441WvWfQUBxKoWj2/m51tF/enBR4D8JLRHifnfJPhTBIUj7bbGFyFBMNbva 0Fj6l0cg8SMyKEt0Q4kD7Wq8ujsi0s8Vpzz/UzXfij/mXgu699rYly19VF5D4s/+uii7kX4 B1HLjcvregTDZIjaasVqei3sy+rrMJJvw62r/LRYwMLODvy3XvgBbhqhMEt/0tHBc6JdpeO kvVlF2PHCeOR276sw1JUM8u7lW3yWKddqN04/bQDZ/8fIi9SITaYBYV78WNW1VnC0YCC0o+ lack9p9DAiXbeMS1dbHou0AXk9300jRX1IkXCBaEySIVyLAqgrr21Nwblk0/A== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 7910942497712437719 From: Song Shuai To: xianting.tian@linux.alibaba.com, mick@ics.forth.gr, heinrich.schuchardt@canonical.com, guoren@kernel.org, k-hagio-ab@nec.com, yixun.lan@gmail.com, lijiang@redhat.com Cc: linux-riscv@lists.infradead.org, kexec@lists.infradead.org, crash-utility@redhat.com, Song Shuai Subject: [Crash-utility PATCH] RISCV64: Add KASLR support Date: Tue, 15 Aug 2023 18:48:00 +0800 Message-Id: <20230815104800.705753-1-songshuaishuai@tinylab.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz4a-0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230815_035033_036450_E7811295 X-CRM114-Status: GOOD ( 14.72 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Song Shuai This patch adds KASLR support for Crash to analyze KASLR-ed vmcore since RISC-V Linux is already sufficiently prepared for KASLR [1]. With this patch, even if the Crash '--kaslr' option is not set or Linux CONFIG_RANDOMIZE_BASE is not configured, the 'derive_kaslr_offset()' function will always work to calculate 'kt->relocate' which serves to update the kernel virtual address. [1]: https://lore.kernel.org/linux-riscv/20230722123850.634544-1-alexghiti@rivosinc.com/ Signed-off-by: Song Shuai Reviewed-by: Guo Ren --- main.c | 2 +- riscv64.c | 11 +++++++++++ symbols.c | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index b278c22..0c6e595 100644 --- a/main.c +++ b/main.c @@ -228,7 +228,7 @@ main(int argc, char **argv) } else if (STREQ(long_options[option_index].name, "kaslr")) { if (!machine_type("X86_64") && !machine_type("ARM64") && !machine_type("X86") && - !machine_type("S390X")) + !machine_type("S390X") && !machine_type("RISCV64")) error(INFO, "--kaslr not valid " "with this machine type.\n"); else if (STREQ(optarg, "auto")) diff --git a/riscv64.c b/riscv64.c index a02f75a..288c7ae 100644 --- a/riscv64.c +++ b/riscv64.c @@ -378,6 +378,9 @@ static void riscv64_get_va_range(struct machine_specific *ms) } else goto error; + if ((kt->flags2 & KASLR) && (kt->flags & RELOC_SET)) + ms->kernel_link_addr += (kt->relocate * -1); + /* * From Linux 5.13, the kernel mapping is moved to the last 2GB * of the address space, modules use the 2GB memory range right @@ -1360,6 +1363,14 @@ riscv64_init(int when) machdep->verify_paddr = generic_verify_paddr; machdep->ptrs_per_pgd = PTRS_PER_PGD; + + /* + * Even if CONFIG_RANDOMIZE_BASE is not configured, + * derive_kaslr_offset() should work and set + * kt->relocate to 0 + */ + if (!kt->relocate && !(kt->flags2 & (RELOC_AUTO|KASLR))) + kt->flags2 |= (RELOC_AUTO|KASLR); break; case PRE_GDB: diff --git a/symbols.c b/symbols.c index 876be7a..8e8b4c3 100644 --- a/symbols.c +++ b/symbols.c @@ -629,7 +629,7 @@ kaslr_init(void) char *string; if ((!machine_type("X86_64") && !machine_type("ARM64") && !machine_type("X86") && - !machine_type("S390X")) || (kt->flags & RELOC_SET)) + !machine_type("S390X") && !machine_type("RISCV64")) || (kt->flags & RELOC_SET)) return; if (!kt->vmcoreinfo._stext_SYMBOL && @@ -795,7 +795,7 @@ store_symbols(bfd *abfd, int dynamic, void *minisyms, long symcount, } else if (!(kt->flags & RELOC_SET)) kt->flags |= RELOC_FORCE; } else if (machine_type("X86_64") || machine_type("ARM64") || - machine_type("S390X")) { + machine_type("S390X") || machine_type("RISCV64")) { if ((kt->flags2 & RELOC_AUTO) && !(kt->flags & RELOC_SET)) derive_kaslr_offset(abfd, dynamic, from, fromend, size, store);