Skip to contentSkip to conversation

RISCV64: Use va_kernel_pa_offset in VTOP()

PATCH In review Song Shuai 2 participants 2 messages Aug 4, 2023 – Aug 15, 2023

Subsystems (3)
lore
Share & tools
lore

Song Shuai

author
#0 lore raw

PATCH v2 1/1 RISCV64: Use va_kernel_pa_offset in VTOP()

Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use PUD/P4D/PGD pages for the linear mapping") changes phys_ram_base from the physical start of the kernel to the actual start of the DRAM.

The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr to translate kernel virtual address, that made Crash boot failed with Linux v6.4 and later version.

Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and backported v6.4.0 stable, so Crash can use "va_kernel_pa_offset" to translate the kernel virtual address in VTOP() correctly.

---
Changes since V1:
- remove unnecessary first kernel version check as Kazu suggested
- amend the commit-msg as Alex suggested

---
 defs.h    |  4 ++--
 riscv64.c | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)
defs.h +2 -2
index 5ee60f1..c07e6d7 100644
@@ -3662,8 +3662,7 @@ typedef signed int s32;
 	ulong _X = X;									\
 	(THIS_KERNEL_VERSION >= LINUX(5,13,0) &&					\
 		(_X) >= machdep->machspec->kernel_link_addr) ?				\
-		(((unsigned long)(_X)-(machdep->machspec->kernel_link_addr)) +		\
-		 machdep->machspec->phys_base):						\
+		((unsigned long)(_X)-(machdep->machspec->va_kernel_pa_offset)): 	\
 		(((unsigned long)(_X)-(machdep->kvbase)) +				\
 		 machdep->machspec->phys_base);						\
 	})
@@ -7021,6 +7020,7 @@ struct machine_specific {
 	ulong modules_vaddr;
 	ulong modules_end;
 	ulong kernel_link_addr;
+	ulong va_kernel_pa_offset;
 
 	ulong _page_present;
 	ulong _page_read;
riscv64.c +23 -0
index 6b9a688..7b5dd3d 100644
@@ -418,6 +418,28 @@ error:
 	error(FATAL, "cannot get vm layout\n");
 }
 
+static void
+riscv64_get_va_kernel_pa_offset(struct machine_specific *ms)
+{
+	unsigned long kernel_version = riscv64_get_kernel_version();
+
+	/*
+	 * Since Linux v6.4 phys_base is not the physical start of the kernel,
+	 * trying to use "va_kernel_pa_offset" to determine the offset between
+	 * kernel virtual and physical addresses.
+	 */
+	if (kernel_version >= LINUX(6,4,0)) {
+		char *string;
+		if ((string = pc->read_vmcoreinfo("NUMBER(va_kernel_pa_offset)"))) {
+			ms->va_kernel_pa_offset = htol(string, QUIET, NULL);
+			free(string);
+		} else
+			error(FATAL, "cannot read va_kernel_pa_offset\n");
+	}
+	else
+		ms->va_kernel_pa_offset = ms->kernel_link_addr - ms->phys_base;
+}
+
 static int
 riscv64_is_kvaddr(ulong vaddr)
 {
@@ -1352,6 +1374,7 @@ riscv64_init(int when)
 		riscv64_get_struct_page_size(machdep->machspec);
 		riscv64_get_va_bits(machdep->machspec);
 		riscv64_get_va_range(machdep->machspec);
+		riscv64_get_va_kernel_pa_offset(machdep->machspec);
 
 		pt_level_alloc(&machdep->pgd, "cannot malloc pgd space.");
 		pt_level_alloc(&machdep->machspec->p4d, "cannot malloc p4d space.");
Signature · 7 lines
2.20.1


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
#1 lore raw
Quoted 12 lines · Song Shuai

Since RISC-V Linux v6.4, the commit 3335068f8721 ("riscv: Use PUD/P4D/PGD pages for the linear mapping") changes phys_ram_base from the physical start of the kernel to the actual start of the DRAM.

The Crash's VTOP() still uses phys_ram_base and kernel_map.virt_addr to translate kernel virtual address, that made Crash boot failed with Linux v6.4 and later version.

Let Linux export kernel_map.va_kernel_pa_offset in v6.5 and backported v6.4.0 stable, so Crash can use "va_kernel_pa_offset" to translate the kernel virtual address in VTOP() correctly.

Quoted 5 lines

---
Changes since V1:
- remove unnecessary first kernel version check as Kazu suggested
- amend the commit-msg as Alex suggested

Thanks for the v2.

Another way is that checking the vmcoreinfo entry before the kernel version, but either is fine to me.

Thanks,
Kazu

Quoted 61 lines
---
  defs.h    |  4 ++--
  riscv64.c | 23 +++++++++++++++++++++++
  2 files changed, 25 insertions(+), 2 deletions(-)
defs.h +2 -2
index 5ee60f1..c07e6d7 100644
@@ -3662,8 +3662,7 @@ typedef signed int s32;
  	ulong _X = X;									\
  	(THIS_KERNEL_VERSION >= LINUX(5,13,0) &&					\
  		(_X) >= machdep->machspec->kernel_link_addr) ?				\
-		(((unsigned long)(_X)-(machdep->machspec->kernel_link_addr)) +		\
-		 machdep->machspec->phys_base):						\
+		((unsigned long)(_X)-(machdep->machspec->va_kernel_pa_offset)): 	\
  		(((unsigned long)(_X)-(machdep->kvbase)) +				\
  		 machdep->machspec->phys_base);						\
  	})
@@ -7021,6 +7020,7 @@ struct machine_specific {
  	ulong modules_vaddr;
  	ulong modules_end;
  	ulong kernel_link_addr;
+	ulong va_kernel_pa_offset;
 
  	ulong _page_present;
  	ulong _page_read;
riscv64.c +22 -0
index 6b9a688..7b5dd3d 100644
@@ -418,6 +418,28 @@ error:
  	error(FATAL, "cannot get vm layout\n");
  }
 
+static void
+riscv64_get_va_kernel_pa_offset(struct machine_specific *ms)
+{
+	unsigned long kernel_version = riscv64_get_kernel_version();
+
+	/*
+	 * Since Linux v6.4 phys_base is not the physical start of the kernel,
+	 * trying to use "va_kernel_pa_offset" to determine the offset between
+	 * kernel virtual and physical addresses.
+	 */
+	if (kernel_version >= LINUX(6,4,0)) {
+		char *string;
+		if ((string = pc->read_vmcoreinfo("NUMBER(va_kernel_pa_offset)"))) {
+			ms->va_kernel_pa_offset = htol(string, QUIET, NULL);
+			free(string);
+		} else
+			error(FATAL, "cannot read va_kernel_pa_offset\n");
+	}
+	else
+		ms->va_kernel_pa_offset = ms->kernel_link_addr - ms->phys_base;
+}
+
  static int
  riscv64_is_kvaddr(ulong vaddr)
  {

riscv64.c riscv64_init(int when) · line 1380

Quoted 7 lines
riscv64.c +1 -0
@@ -1352,6 +1374,7 @@ riscv64_init(int when)
  		riscv64_get_struct_page_size(machdep->machspec);
  		riscv64_get_va_bits(machdep->machspec);
  		riscv64_get_va_range(machdep->machspec);
+		riscv64_get_va_kernel_pa_offset(machdep->machspec);
 
  		pt_level_alloc(&machdep->pgd, "cannot malloc pgd space.");
  		pt_level_alloc(&machdep->machspec->p4d, "cannot malloc p4d space.");

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec