From eb5a41cf2fe89a28bbbbff05fd2d22af287207c6 Mon Sep 17 00:00:00 2001 From: Leandro Lupori Date: Wed, 5 Feb 2020 11:34:10 +0000 Subject: [PATCH] Add SYSCTL to get KERNBASE and relocated KERNBASE This change adds 2 new SYSCTLs, to retrieve the original and relocated KERNBASE values. This provides an easy, architecture independent way to calculate the running kernel displacement (current/load address minus original base address). The initial goal for this change is to add a new libkvm function that returns the kernel displacement, both for live kernels and crashdumps. This would in turn be used by kgdb to find out how to relocate kernel symbols (if needed). Reviewed by: jhb Differential Revision: https://reviews.freebsd.org/D23284 --- sys/kern/link_elf.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sys/kern/link_elf.c b/sys/kern/link_elf.c index 03f9ae63d9a..266b9d0b06b 100644 --- a/sys/kern/link_elf.c +++ b/sys/kern/link_elf.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include @@ -389,6 +390,13 @@ link_elf_link_common_finish(linker_file_t lf) extern vm_offset_t __startkernel, __endkernel; +static unsigned long kern_relbase = KERNBASE; + +SYSCTL_ULONG(_kern, OID_AUTO, base_address, CTLFLAG_RD, + SYSCTL_NULL_ULONG_PTR, KERNBASE, "Kernel base address"); +SYSCTL_ULONG(_kern, OID_AUTO, relbase_address, CTLFLAG_RD, + &kern_relbase, 0, "Kernel relocated base address"); + static void link_elf_init(void* arg) { @@ -431,6 +439,7 @@ link_elf_init(void* arg) #ifdef __powerpc__ linker_kernel_file->address = (caddr_t)__startkernel; linker_kernel_file->size = (intptr_t)(__endkernel - __startkernel); + kern_relbase = (unsigned long)__startkernel; #else linker_kernel_file->address += KERNBASE; linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;