From patchwork Fri Aug 18 09:50:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Song Shuai X-Patchwork-Id: 13357617 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 61C50C678DC for ; Fri, 18 Aug 2023 09:52:22 +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=IYOTpimdxfe70RbfgXx4zZYCXLcwI6TJ2FjyuiJkwaU=; b=1O49vLG6LnSwzq INqyvqSA7e8k24mcUiZePmFV4RauqPYcnAYfFJLKEeyMwRt7orAGjvvWUaXz5sW5SUHycZus6Y4H8 P7/6XnUKPWo0Ru5RnlRwv1V81mtBXO3EqDqGL3hcP+j2cBYzqwUBFeBQEV4jABVWnZOkuh4e2iLFJ n4y0BvYiTfQgjNhyksQUjYRMQtvUpa1F0MsCeOA2gw2551AAxGKAQjg1Y2ERf7ts+Ma58bBGdSNRs 2uFIbsnYsy9KCAOjQe/sxvctRYGuBkYFnUxbiaZAs8OJ93ifHP9ZpsrOL9XuWe1D7MSAk9vvjSmDu mN7HwRVZs4UXUxmW1Mng==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qWw9P-0088yl-04; Fri, 18 Aug 2023 09:52:15 +0000 Received: from bg4.exmail.qq.com ([43.154.221.58]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qWw9J-0088wY-0D; Fri, 18 Aug 2023 09:52:11 +0000 X-QQ-mid: bizesmtp89t1692352231tywfa1bm Received: from localhost.localdomain ( [61.183.83.60]) by bizesmtp.qq.com (ESMTP) with id ; Fri, 18 Aug 2023 17:50:30 +0800 (CST) X-QQ-SSF: 01200000000000D0D000000A0000000 X-QQ-FEAT: xnbq7qFd8vprDzjZChdw+TNRpRXlOB7G0+uO75guUGIV9Cu0rivaXtLi2NhCg xrjv3NtDi7yI394qgiVmxqCe0u6+vEC/XDgJhxv8QRze0L4BGdCLahk2d+3cHMGRsyeh9D8 DCvHwT/hb994F7PomV6YEfQBmZFcOf3NLwFRDIODZEiuM1Y5tcmp7OzT1b7mX1XXExtIlec hj1m3LogqZ3AjGWr77m/f9gdzavMjF5KuEOQAM8jtCwW0zgTUkI7a2PZM8N96z1SSC44hAQ UsZzkcmHBUTcqeabrEDehOmqJgeCw09cWaBUGWLt5165G5Dy633nN1HL0Jfi15vcg2SBtSK uj5SE8FDmzcH5lJX5yeTg6m56m3eC9Bv8UaYnW5 X-QQ-GoodBg: 0 X-BIZMAIL-ID: 6813891064426468315 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 V2] RISCV64: Add KASLR support Date: Fri, 18 Aug 2023 17:50:28 +0800 Message-Id: <20230818095028.1012639-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-20230818_025209_453215_8B3DB010 X-CRM114-Status: GOOD ( 15.94 ) 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. Testing in Qemu rv64 virt, kernel log outputed the kernel offset: [ 121.214447] SMP: stopping secondary CPUs [ 121.215445] Kernel Offset: 0x37c00000 from 0xffffffff80000000 [ 121.216312] Starting crashdump kernel... [ 121.216585] Will call new kernel at 94800000 from hart id 0 [ 121.216834] FDT image at 9c7fd000 [ 121.216982] Bye... Running crash with `-d 1` option and without `--kaslr` option, we get the right `kt->relocate` and kernel link addr: $ ../crash/crash -d 1 vmlinux vmcore_kaslr_0815 ... KASLR: _stext from vmlinux: ffffffff80002000 _stext from vmcoreinfo: ffffffffb7c02000 relocate: 37c00000 (892MB) vmemmap : 0xff1c000000000000 - 0xff20000000000000 vmalloc : 0xff20000000000000 - 0xff60000000000000 mudules : 0xffffffff3952f000 - 0xffffffffb7c00000 lowmem : 0xff60000000000000 - kernel link addr : 0xffffffffb7c00000 ... KERNEL: /home/song/9_linux/linux/00_rv_kaslr/vmlinux DUMPFILE: /tmp/hello/vmcore_kaslr_0815 CPUS: 2 DATE: Tue Aug 15 16:36:15 CST 2023 UPTIME: 00:02:01 LOAD AVERAGE: 0.40, 0.23, 0.09 TASKS: 63 NODENAME: stage4.fedoraproject.org RELEASE: 6.5.0-rc3-00008-gad18dee423ac VERSION: #17 SMP Tue Aug 15 14:41:12 CST 2023 MACHINE: riscv64 (unknown Mhz) MEMORY: 511.8 MB PANIC: "Kernel panic - not syncing: sysrq triggered crash" PID: 160 COMMAND: "bash" TASK: ff6000000152bac0 [THREAD_INFO: ff6000000152bac0] CPU: 1 STATE: TASK_RUNNING (PANIC) crash> [1]: https://lore.kernel.org/linux-riscv/20230722123850.634544-1-alexghiti@rivosinc.com/ Signed-off-by: Song Shuai Reviewed-by: Guo Ren --- Changes since V1: https://lore.kernel.org/linux-riscv/20230815104800.705753-1-songshuaishuai@tinylab.org/ - supplement the output of my Crash test in the commit-msg - add the Reviewed-by from Guo --- 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);