Blame SOURCES/kexec-tools-2.0.15-makedumpfile-arm64-Add-support-to-read-symbols-like-_stext-from-p.patch

e79652
From d1e780507b6ed4b67e5e0e4e4b7c9796dc537210 Mon Sep 17 00:00:00 2001
e79652
From: Bhupesh Sharma <bhsharma@redhat.com>
e79652
Date: Tue, 6 Mar 2018 02:13:00 +0900
e79652
Subject: [PATCH 2/3] arm64: Add support to read symbols like _stext from
e79652
 '/proc/kallsyms'
e79652
e79652
On ARM64 platforms the VA_BITS supported by a linux kernel being run
e79652
can be selected by setting 'ARM64_VA_BITS_*' (see 'arch/arm64/Kconfig'
e79652
for details).
e79652
e79652
Now, to determine the 'info->page_offset' in arm64 makedumpfile
e79652
context ('arch/arm64.c') we need to determine the VA_BITS which was
e79652
selected by the underlying linux kernel.
e79652
e79652
There can be several ways to determine the VA_BITS, out of which
e79652
reading the '_stext' symbol and calculating the 'va_bits' using the
e79652
same is the simplest yet portable method.
e79652
e79652
For reading the kernel symbols like '_stext', we can read the
e79652
'/proc/kallsyms' file which contains these symbols and
e79652
works even in case of KASLR enabled arm64 kernels, as in those cases,
e79652
the '_stext' symbol will end up being randomized and hence
e79652
cannot be correctly read from the 'vmlinux' file.
e79652
e79652
Signed-off-by: Bhupesh Sharma <bhsharma@redhat.com>
e79652
---
e79652
 arch/arm64.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
e79652
 1 file changed, 73 insertions(+), 4 deletions(-)
e79652
e79652
diff --git a/makedumpfile-1.6.2/arch/arm64.c b/makedumpfile-1.6.2/arch/arm64.c
e79652
index 25d7a1f4db98..c9dab677f2c9 100644
e79652
--- a/makedumpfile-1.6.2/arch/arm64.c
e79652
+++ b/makedumpfile-1.6.2/arch/arm64.c
e79652
@@ -48,6 +48,12 @@ static unsigned long kimage_voffset;
e79652
 #define SZ_64K			(64 * 1024)
e79652
 #define SZ_128M			(128 * 1024 * 1024)
e79652
 
e79652
+#define PAGE_OFFSET_36 ((0xffffffffffffffffUL) << 36)
e79652
+#define PAGE_OFFSET_39 ((0xffffffffffffffffUL) << 39)
e79652
+#define PAGE_OFFSET_42 ((0xffffffffffffffffUL) << 42)
e79652
+#define PAGE_OFFSET_47 ((0xffffffffffffffffUL) << 47)
e79652
+#define PAGE_OFFSET_48 ((0xffffffffffffffffUL) << 48)
e79652
+
e79652
 #define pgd_val(x)		((x).pgd)
e79652
 #define pud_val(x)		(pgd_val((x).pgd))
e79652
 #define pmd_val(x)		(pud_val((x).pud))
e79652
@@ -140,8 +146,6 @@ pud_offset(pgd_t *pgda, pgd_t *pgdv, unsigned long vaddr)
e79652
 
e79652
 static int calculate_plat_config(void)
e79652
 {
e79652
-	va_bits = NUMBER(VA_BITS);
e79652
-
e79652
 	/* derive pgtable_level as per arch/arm64/Kconfig */
e79652
 	if ((PAGESIZE() == SZ_16K && va_bits == 36) ||
e79652
 			(PAGESIZE() == SZ_64K && va_bits == 42)) {
e79652
@@ -177,6 +181,44 @@ get_phys_base_arm64(void)
e79652
 	return TRUE;
e79652
 }
e79652
 
e79652
+ulong
e79652
+get_stext_symbol(void)
e79652
+{
e79652
+	int found;
e79652
+	FILE *fp;
e79652
+	char buf[BUFSIZE];
e79652
+	char *kallsyms[MAXARGS];
e79652
+	ulong kallsym;
e79652
+
e79652
+	if (!file_exists("/proc/kallsyms")) {
e79652
+		ERRMSG("(%s) does not exist, will not be able to read symbols. %s\n",
e79652
+		       "/proc/kallsyms", strerror(errno));
e79652
+		return FALSE;
e79652
+	}
e79652
+
e79652
+	if ((fp = fopen("/proc/kallsyms", "r")) == NULL) {
e79652
+		ERRMSG("Cannot open (%s) to read symbols. %s\n",
e79652
+		       "/proc/kallsyms", strerror(errno));
e79652
+		return FALSE;
e79652
+	}
e79652
+
e79652
+	found = FALSE;
e79652
+	kallsym = 0;
e79652
+
e79652
+	while (!found && fgets(buf, BUFSIZE, fp) &&
e79652
+	      (parse_line(buf, kallsyms) == 3)) {
e79652
+		if (hexadecimal(kallsyms[0], 0) &&
e79652
+		    STREQ(kallsyms[2], "_stext")) {
e79652
+			kallsym = htol(kallsyms[0], 0);
e79652
+			found = TRUE;
e79652
+			break;
e79652
+		}
e79652
+	}
e79652
+	fclose(fp);
e79652
+
e79652
+	return(found ? kallsym : FALSE);
e79652
+}
e79652
+
e79652
 int
e79652
 get_machdep_info_arm64(void)
e79652
 {
e79652
@@ -188,12 +230,10 @@ get_machdep_info_arm64(void)
e79652
 	kimage_voffset = NUMBER(kimage_voffset);
e79652
 	info->max_physmem_bits = PHYS_MASK_SHIFT;
e79652
 	info->section_size_bits = SECTIONS_SIZE_BITS;
e79652
-	info->page_offset = 0xffffffffffffffffUL << (va_bits - 1);
e79652
 
e79652
 	DEBUG_MSG("kimage_voffset   : %lx\n", kimage_voffset);
e79652
 	DEBUG_MSG("max_physmem_bits : %lx\n", info->max_physmem_bits);
e79652
 	DEBUG_MSG("section_size_bits: %lx\n", info->section_size_bits);
e79652
-	DEBUG_MSG("page_offset      : %lx\n", info->page_offset);
e79652
 
e79652
 	return TRUE;
e79652
 }
e79652
@@ -219,6 +259,35 @@ get_xen_info_arm64(void)
e79652
 int
e79652
 get_versiondep_info_arm64(void)
e79652
 {
e79652
+	ulong _stext;
e79652
+
e79652
+	_stext = get_stext_symbol();
e79652
+	if (!_stext) {
e79652
+		ERRMSG("Can't get the symbol of _stext.\n");
e79652
+		return FALSE;
e79652
+	}
e79652
+
e79652
+	/* Derive va_bits as per arch/arm64/Kconfig */
e79652
+	if ((_stext & PAGE_OFFSET_36) == PAGE_OFFSET_36) {
e79652
+		va_bits = 36;
e79652
+	} else if ((_stext & PAGE_OFFSET_39) == PAGE_OFFSET_39) {
e79652
+		va_bits = 39;
e79652
+	} else if ((_stext & PAGE_OFFSET_42) == PAGE_OFFSET_42) {
e79652
+		va_bits = 42;
e79652
+	} else if ((_stext & PAGE_OFFSET_47) == PAGE_OFFSET_47) {
e79652
+		va_bits = 47;
e79652
+	} else if ((_stext & PAGE_OFFSET_48) == PAGE_OFFSET_48) {
e79652
+		va_bits = 48;
e79652
+	} else {
e79652
+		ERRMSG("Cannot find a proper _stext for calculating VA_BITS\n");
e79652
+		return FALSE;
e79652
+	}
e79652
+
e79652
+	info->page_offset = (0xffffffffffffffffUL) << (va_bits - 1);
e79652
+
e79652
+	DEBUG_MSG("page_offset=%lx, va_bits=%d\n", info->page_offset,
e79652
+			va_bits);
e79652
+
e79652
 	return TRUE;
e79652
 }
e79652
 
e79652
-- 
e79652
2.7.4
e79652