From 13de0512b5aff94bde8e45204e9bc2dcb681c515 Mon Sep 17 00:00:00 2001 From: Yueyi Li Date: Tue, 14 Mar 2017 21:25:21 +0800 Subject: [PATCH] [ARM64][patch] Auto calculate kimage_voffset by kaslr offset ARM64 kimage_voffset can be calculated if kernel ASLR offset is known. Add a function to auto calculate kimage_voffset when '--kaslr=' was set. --- arm64.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/arm64.c b/arm64.c index 6eaf96d..9ff9428 100644 --- a/arm64.c +++ b/arm64.c @@ -27,6 +27,7 @@ static struct machine_specific arm64_machine_specific = { 0 }; static int arm64_verify_symbol(const char *, ulong, char); static void arm64_parse_cmdline_args(void); +static void arm64_calc_kimage_voffset(void); static void arm64_calc_phys_offset(void); static void arm64_calc_virtual_memory_ranges(void); static int arm64_kdump_phys_base(ulong *); @@ -324,6 +325,9 @@ arm64_init(int when) machdep->init_kernel_pgd = arm64_init_kernel_pgd; /* use machdep parameters */ + arm64_calc_kimage_voffset(); + + /* use machdep parameters */ arm64_calc_phys_offset(); if (CRASHDEBUG(1)) { @@ -735,6 +739,68 @@ arm64_parse_cmdline_args(void) } } +static void +arm64_calc_kimage_voffset(void) +{ + struct machine_specific *ms = machdep->machspec; + ulong phys_offset; + + if (ms->kimage_voffset) /* vmcoreinfo or --machdep override */ + return; + + if (!(kt->flags2 & KASLR) || !(kt->flags & RELOC_SET)) /*Calculate kiamge_voffset when KASLR enabled.*/ + return; + + if(ACTIVE()){ + char buf[BUFSIZE]; + char *p1; + int errflag; + FILE *iomem; + + if ((iomem = fopen("/proc/iomem", "r")) == NULL) + return; + + /* + * Memory regions are sorted in ascending order. We take the + * first region which should be correct for most uses. + */ + errflag = 1; + while (fgets(buf, BUFSIZE, iomem)) { + if (strstr(buf, ": System RAM")) { + clean_line(buf); + errflag = 0; + break; + } + } + fclose(iomem); + + if (errflag) + return; + + if (!(p1 = strstr(buf, "-"))) + return; + + *p1 = NULLCHAR; + + phys_offset = htol(buf, RETURN_ON_ERROR | QUIET, &errflag); + if (errflag) + return; + }else if (DISKDUMP_DUMPFILE()) + return; + else if (KDUMP_DUMPFILE()) + arm_kdump_phys_base(&phys_offset); /*Get start address of first memory block*/ + else{ + error(WARNING, + "kimage_voffset cannot be determined from the dumpfile.\n"); + error(CONT, + "Using default value of 0. If this is not correct, then try\n"); + error(CONT, + "using the command line option: --machdep kimage_voffset=\n"); + return; + } + + ms->kimage_voffset = ms->vmalloc_start_addr + (kt->relocate * -1) - phys_offset; +} static void arm64_calc_phys_offset(void) -- 1.9.1