From 8515b0c616d782faababae933ee637e843c7f197 Mon Sep 17 00:00:00 2001
From: chenguanyou <chenguanyou@xiaomi.com>
Date: Thu, 8 Aug 2024 17:35:07 +0800
Subject: [PATCH] Fix: arm64: section_size_bits compatible with macro
 definitions

The commit 568c6f04 will cause a regression issue for the determination of
section_size_bits on kernel version before android12-5.10 or Linux-v5.12.
The section_size_bits is supposed to be compatible with linux upstream and
android GKI version:
Before android12-5.10 or Linux-v5.12:
        SECTION_SIZE_BITS = 30

After android12-5.10 or Linux-v5.12:
    SECTION_SIZE_BITS = 27 when defined 4K_PAGES or 16K_PAGES.
    SECTION_SIZE_BITS = 29 when defined 64K_PAGES.

Fixes: 568c6f04 ("arm64: section_size_bits compatible with macro definitions")
Change-Id: Ib6ec610753aabb52dfbafab65e1e6c04cbe65f72
Signed-off-by: qiwu.chen <qiwu.chen@transsion.com>
Signed-off-by: chenguanyou <chenguanyou@xiaomi.com>
---
 arm64.c       | 22 ++++++++++------------
 defs.h        | 10 ++++++++++
 global_data.c |  7 +++++++
 kernel.c      | 23 +++++++++++++++++++++++
 4 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/arm64.c b/arm64.c
index 8ed1aaf..f0721b7 100644
--- a/arm64.c
+++ b/arm64.c
@@ -1667,30 +1667,28 @@ arm64_get_section_size_bits(void)
 	int ret;
 	char *string;
 
-	if (THIS_KERNEL_VERSION >= LINUX(5,12,0)) {
+	if (arm64_get_vmcoreinfo(&machdep->section_size_bits, "NUMBER(SECTION_SIZE_BITS)", NUM_DEC))
+		goto exit;
+
+	if (THIS_KERNEL_VERSION >= LINUX(5,12,0)
+			|| (IS_ANDROID_KERNEL_REL
+				&& THIS_KERNEL_VERSION >= LINUX(5,10,0)
+				&& THIS_ANDROID_VERSION >= ANDROID(12,0)))
 		if (machdep->pagesize == 65536)
 			machdep->section_size_bits = _SECTION_SIZE_BITS_5_12_64K;
 		else
 			machdep->section_size_bits = _SECTION_SIZE_BITS_5_12;
-	} else
+	else
 		machdep->section_size_bits = _SECTION_SIZE_BITS;
 
-	if (arm64_get_vmcoreinfo(&machdep->section_size_bits, "NUMBER(SECTION_SIZE_BITS)", NUM_DEC)) {
-		/* nothing */
-	} else if (kt->ikconfig_flags & IKCONFIG_AVAIL) {
+	if (kt->ikconfig_flags & IKCONFIG_AVAIL) {
 		if ((ret = get_kernel_config("CONFIG_MEMORY_HOTPLUG", NULL)) == IKCONFIG_Y) {
 			if ((ret = get_kernel_config("CONFIG_HOTPLUG_SIZE_BITS", &string)) == IKCONFIG_STR)
 				machdep->section_size_bits = atol(string);
 		}
-
-		/* arm64: reduce section size for sparsemem */
-		if ((ret = get_kernel_config("CONFIG_ARM64_4K_PAGES", NULL)) == IKCONFIG_Y
-			|| (ret = get_kernel_config("CONFIG_ARM64_16K_PAGES", NULL)) == IKCONFIG_Y)
-			machdep->section_size_bits = _SECTION_SIZE_BITS_5_12;
-		else if ((ret = get_kernel_config("CONFIG_ARM64_64K_PAGES", NULL)) == IKCONFIG_Y)
-			machdep->section_size_bits = _SECTION_SIZE_BITS_5_12_64K;
 	}
 
+exit:
 	if (CRASHDEBUG(1))
 		fprintf(fp, "SECTION_SIZE_BITS: %ld\n", machdep->section_size_bits);
 }
diff --git a/defs.h b/defs.h
index 1b7649d..b0d3f8f 100644
--- a/defs.h
+++ b/defs.h
@@ -829,6 +829,15 @@ struct kernel_table {                   /* kernel data */
 #define IS_KERNEL_STATIC_TEXT(x) (((ulong)(x) >= kt->stext) && \
 		  	          ((ulong)(x) < kt->etext))
 
+#define THIS_KERNEL_RELEASE (kt->utsname.release)
+
+struct android_table {
+	uint android_version[2];
+};
+#define IS_ANDROID_KERNEL_REL (at->android_version[0] > 0)
+#define THIS_ANDROID_VERSION ((at->android_version[0] << 16) + (at->android_version[1]))
+#define ANDROID(x,y) (((uint)(x) << 16) + (uint)(y))
+
 #define TASK_COMM_LEN 16     /* task command name length including NULL */
 
 struct task_context {                     /* context stored for each task */
@@ -5353,6 +5362,7 @@ extern struct vm_table vm_table, *vt;
 extern struct machdep_table *machdep;
 extern struct symbol_table_data symbol_table_data, *st;
 extern struct extension_table *extension_table;
+extern struct android_table android_table, *at;
 
 /*
  *  Generated in build_data.c
diff --git a/global_data.c b/global_data.c
index f9bb7d0..dddde96 100644
--- a/global_data.c
+++ b/global_data.c
@@ -55,6 +55,13 @@ struct symbol_table_data *st = &symbol_table_data;
 struct machdep_table machdep_table = { 0 };
 struct machdep_table *machdep = &machdep_table;
 
+/*
+ *  The same thing goes for accesses to the frequently-accessed android_table,
+ *  making the "at" pointers globally available.
+ */
+struct android_table android_table = { 0, 0 };
+struct android_table *at = &android_table;
+
 /*
  *  Command functions are entered with the args[] array and argcnt value 
  *  pre-set for issuance to getopt().
diff --git a/kernel.c b/kernel.c
index adb19ad..814418c 100644
--- a/kernel.c
+++ b/kernel.c
@@ -104,6 +104,26 @@ static void check_vmcoreinfo(void);
 static int is_pvops_xen(void);
 static int get_linux_banner_from_vmlinux(char *, size_t);
 
+/*
+ * Determine Andriod GKI vmcore by reading "android" from ut->release.
+ * The prefix of Andriod GKI release version is:
+ *     Kernel Version - Android release version
+ * For example:
+ *     5.10.209-android12, 5.10.209-android12.1, 5.15.148-android13
+ */
+void
+parse_android_table(void)
+{
+    char *p;
+    if ((p = strstr(THIS_KERNEL_RELEASE, "android"))) {
+        sscanf(p, "android%d.%d", &at->android_version[0], &at->android_version[1]);
+
+        if (CRASHDEBUG(1))
+            fprintf(fp, "andriod_version: andriod-%d.%d\n",
+                    at->android_version[0], at->android_version[1]);
+    }
+}
+
 /*
  * popuplate the global kernel table (kt) with kernel version
  * information parsed from UTSNAME/OSRELEASE string
@@ -298,6 +318,9 @@ kernel_init()
 			kt->utsname.domainname : "(not printable)");
 	}
 
+	// non-upstream rel
+	parse_android_table();
+
 	strncpy(buf, kt->utsname.release, 65);
 	if (buf[64])
 		buf[64] = NULLCHAR;
-- 
2.39.0

