[PATCH RFC 8/8] riscv/kaslr: dump out kernel offset information on panic

From: Zong Li
Date: Tue Mar 24 2020 - 03:31:16 EST


Dump out the kernel offset when panic to help debug kernel.

Signed-off-by: Zong Li <zong.li@xxxxxxxxxx>
---
arch/riscv/kernel/setup.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c
index 913d25e4b9fa..3ce50bf628ba 100644
--- a/arch/riscv/kernel/setup.c
+++ b/arch/riscv/kernel/setup.c
@@ -85,3 +85,26 @@ void __init setup_arch(char **cmdline_p)

riscv_fill_hwcap();
}
+
+static int dump_kernel_offset(struct notifier_block *self, unsigned long v,
+ void *p)
+{
+ pr_emerg("Kernel Offset: 0x%lx from 0x%lx\n",
+ get_kaslr_offset(), PAGE_OFFSET);
+
+ return 0;
+}
+
+static struct notifier_block kernel_offset_notifier = {
+ .notifier_call = dump_kernel_offset
+};
+
+static int __init register_kernel_offset_dumper(void)
+{
+ if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && get_kaslr_offset() > 0)
+ atomic_notifier_chain_register(&panic_notifier_list,
+ &kernel_offset_notifier);
+
+ return 0;
+}
+__initcall(register_kernel_offset_dumper);
--
2.25.1