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
This commit is contained in:
Leandro Lupori 2020-02-05 11:34:10 +00:00
parent 7503958e4c
commit eb5a41cf2f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=357570

View File

@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/linker.h>
#include <sys/sysctl.h>
#include <machine/elf.h>
@ -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;