Re: [PATCH v3 4/5] RISC-V: Add kdump support

2021-04-09 Thread Nick Kossifidis

Στις 2021-04-06 21:36, Alex Ghiti έγραψε:



+   /* Switch to physical addressing */
+   la  s4, 1f
+   sub s4, s4, s3
+   csrwstvec, s4
+   csrwsptbr, zero


satp is used everywhere instead of sptbr. And maybe you could CSR_
naming, like you did in riscv_crash_save_regs and like it's done in
head.S too.



ACK


+   crash_base = memblock_find_in_range(search_start, search_end,
+#ifdef CONFIG_64BIT
+   crash_size, SZ_2M);
+#else
+   crash_size, SZ_4M);
+#endif


You can use PMD_SIZE here and get rid of #ifdef.


+
+#ifdef CONFIG_64BIT
+   if (!IS_ALIGNED(crash_base, SZ_2M)) {
+#else
+   if (!IS_ALIGNED(crash_base, SZ_4M)) {
+#endif


Ditto here.



Will do.

Thanks a lot for your review !


Re: [PATCH v3 4/5] RISC-V: Add kdump support

2021-04-06 Thread Alex Ghiti

Hi Nick,

Le 4/5/21 à 4:57 AM, Nick Kossifidis a écrit :

This patch adds support for kdump, the kernel will reserve a
region for the crash kernel and jump there on panic. In order
for userspace tools (kexec-tools) to prepare the crash kernel
kexec image, we also need to expose some information on
/proc/iomem for the memory regions used by the kernel and for
the region reserved for crash kernel. Note that on userspace
the device tree is used to determine the system's memory
layout so the "System RAM" on /proc/iomem is ignored.

I tested this on riscv64 qemu and works as expected, you may
test it by triggering a crash through /proc/sysrq_trigger:

echo c > /proc/sysrq_trigger

v3:
  * Move ELF_CORE_COPY_REGS to asm/elf.h instead of uapi/asm/elf.h
  * Set stvec when disabling MMU
  * Minor cleanups and re-base

v2:
  * Properly populate the ioresources tree, so that it can be
used later on for implementing strict /dev/mem.
  * Minor cleanups and re-base

Signed-off-by: Nick Kossifidis 
---
  arch/riscv/include/asm/elf.h|  6 +++
  arch/riscv/include/asm/kexec.h  | 19 ---
  arch/riscv/kernel/Makefile  |  2 +-
  arch/riscv/kernel/crash_save_regs.S | 56 +
  arch/riscv/kernel/kexec_relocate.S  | 68 -
  arch/riscv/kernel/machine_kexec.c   | 43 +---
  arch/riscv/kernel/setup.c   | 11 -
  arch/riscv/mm/init.c| 77 +
  8 files changed, 255 insertions(+), 27 deletions(-)
  create mode 100644 arch/riscv/kernel/crash_save_regs.S

diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h
index 5c725e1df..f4b490cd0 100644
--- a/arch/riscv/include/asm/elf.h
+++ b/arch/riscv/include/asm/elf.h
@@ -81,4 +81,10 @@ extern int arch_setup_additional_pages(struct linux_binprm 
*bprm,
int uses_interp);
  #endif /* CONFIG_MMU */
  
+#define ELF_CORE_COPY_REGS(dest, regs)			\

+do {   \
+   *(struct user_regs_struct *)&(dest) =   \
+   *(struct user_regs_struct *)regs;   \
+} while (0);
+
  #endif /* _ASM_RISCV_ELF_H */
diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h
index efc69feb4..4fd583acc 100644
--- a/arch/riscv/include/asm/kexec.h
+++ b/arch/riscv/include/asm/kexec.h
@@ -21,11 +21,16 @@
  
  #define KEXEC_ARCH KEXEC_ARCH_RISCV
  
+extern void riscv_crash_save_regs(struct pt_regs *newregs);

+
  static inline void
  crash_setup_regs(struct pt_regs *newregs,
 struct pt_regs *oldregs)
  {
-   /* Dummy implementation for now */
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(struct pt_regs));
+   else
+   riscv_crash_save_regs(newregs);
  }
  
  
@@ -38,10 +43,12 @@ struct kimage_arch {

  const extern unsigned char riscv_kexec_relocate[];
  const extern unsigned int riscv_kexec_relocate_size;
  
-typedef void (*riscv_kexec_do_relocate)(unsigned long first_ind_entry,

-   unsigned long jump_addr,
-   unsigned long fdt_addr,
-   unsigned long hartid,
-   unsigned long va_pa_off);
+typedef void (*riscv_kexec_method)(unsigned long first_ind_entry,
+  unsigned long jump_addr,
+  unsigned long fdt_addr,
+  unsigned long hartid,
+  unsigned long va_pa_off);
+
+extern riscv_kexec_method riscv_kexec_norelocate;
  
  #endif

diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index c2594018c..07f676ad3 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -58,7 +58,7 @@ obj-$(CONFIG_SMP) += cpu_ops_sbi.o
  endif
  obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o
  obj-$(CONFIG_KGDB)+= kgdb.o
-obj-${CONFIG_KEXEC}+= kexec_relocate.o machine_kexec.o
+obj-${CONFIG_KEXEC}+= kexec_relocate.o crash_save_regs.o 
machine_kexec.o
  
  obj-$(CONFIG_JUMP_LABEL)	+= jump_label.o
  
diff --git a/arch/riscv/kernel/crash_save_regs.S b/arch/riscv/kernel/crash_save_regs.S

new file mode 100644
index 0..7832fb763
--- /dev/null
+++ b/arch/riscv/kernel/crash_save_regs.S
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 FORTH-ICS/CARV
+ *  Nick Kossifidis 
+ */
+
+#include  /* For RISCV_* and REG_* macros */
+#include  /* For CSR_* macros */
+#include  /* For offsets on pt_regs */
+#include/* For SYM_* macros */
+
+.section ".text"
+SYM_CODE_START(riscv_crash_save_regs)
+   REG_S ra,  PT_RA(a0)/* x1 */
+   REG_S sp,  PT_SP(a0)/* x2 */
+   REG_S gp,  PT_GP(a0)/* x3 */
+   REG_S tp,  PT_TP(a0)/* x4 */
+   REG_S t0,  PT_T0(a0)/* x5 */
+   REG_S t1,  PT_T1(a0)/* x6 */
+   REG_S t2,  PT_

[PATCH v3 4/5] RISC-V: Add kdump support

2021-04-05 Thread Nick Kossifidis
This patch adds support for kdump, the kernel will reserve a
region for the crash kernel and jump there on panic. In order
for userspace tools (kexec-tools) to prepare the crash kernel
kexec image, we also need to expose some information on
/proc/iomem for the memory regions used by the kernel and for
the region reserved for crash kernel. Note that on userspace
the device tree is used to determine the system's memory
layout so the "System RAM" on /proc/iomem is ignored.

I tested this on riscv64 qemu and works as expected, you may
test it by triggering a crash through /proc/sysrq_trigger:

echo c > /proc/sysrq_trigger

v3:
 * Move ELF_CORE_COPY_REGS to asm/elf.h instead of uapi/asm/elf.h
 * Set stvec when disabling MMU
 * Minor cleanups and re-base

v2:
 * Properly populate the ioresources tree, so that it can be
   used later on for implementing strict /dev/mem.
 * Minor cleanups and re-base

Signed-off-by: Nick Kossifidis 
---
 arch/riscv/include/asm/elf.h|  6 +++
 arch/riscv/include/asm/kexec.h  | 19 ---
 arch/riscv/kernel/Makefile  |  2 +-
 arch/riscv/kernel/crash_save_regs.S | 56 +
 arch/riscv/kernel/kexec_relocate.S  | 68 -
 arch/riscv/kernel/machine_kexec.c   | 43 +---
 arch/riscv/kernel/setup.c   | 11 -
 arch/riscv/mm/init.c| 77 +
 8 files changed, 255 insertions(+), 27 deletions(-)
 create mode 100644 arch/riscv/kernel/crash_save_regs.S

diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h
index 5c725e1df..f4b490cd0 100644
--- a/arch/riscv/include/asm/elf.h
+++ b/arch/riscv/include/asm/elf.h
@@ -81,4 +81,10 @@ extern int arch_setup_additional_pages(struct linux_binprm 
*bprm,
int uses_interp);
 #endif /* CONFIG_MMU */
 
+#define ELF_CORE_COPY_REGS(dest, regs) \
+do {   \
+   *(struct user_regs_struct *)&(dest) =   \
+   *(struct user_regs_struct *)regs;   \
+} while (0);
+
 #endif /* _ASM_RISCV_ELF_H */
diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h
index efc69feb4..4fd583acc 100644
--- a/arch/riscv/include/asm/kexec.h
+++ b/arch/riscv/include/asm/kexec.h
@@ -21,11 +21,16 @@
 
 #define KEXEC_ARCH KEXEC_ARCH_RISCV
 
+extern void riscv_crash_save_regs(struct pt_regs *newregs);
+
 static inline void
 crash_setup_regs(struct pt_regs *newregs,
 struct pt_regs *oldregs)
 {
-   /* Dummy implementation for now */
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(struct pt_regs));
+   else
+   riscv_crash_save_regs(newregs);
 }
 
 
@@ -38,10 +43,12 @@ struct kimage_arch {
 const extern unsigned char riscv_kexec_relocate[];
 const extern unsigned int riscv_kexec_relocate_size;
 
-typedef void (*riscv_kexec_do_relocate)(unsigned long first_ind_entry,
-   unsigned long jump_addr,
-   unsigned long fdt_addr,
-   unsigned long hartid,
-   unsigned long va_pa_off);
+typedef void (*riscv_kexec_method)(unsigned long first_ind_entry,
+  unsigned long jump_addr,
+  unsigned long fdt_addr,
+  unsigned long hartid,
+  unsigned long va_pa_off);
+
+extern riscv_kexec_method riscv_kexec_norelocate;
 
 #endif
diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile
index c2594018c..07f676ad3 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -58,7 +58,7 @@ obj-$(CONFIG_SMP) += cpu_ops_sbi.o
 endif
 obj-$(CONFIG_HOTPLUG_CPU)  += cpu-hotplug.o
 obj-$(CONFIG_KGDB) += kgdb.o
-obj-${CONFIG_KEXEC}+= kexec_relocate.o machine_kexec.o
+obj-${CONFIG_KEXEC}+= kexec_relocate.o crash_save_regs.o 
machine_kexec.o
 
 obj-$(CONFIG_JUMP_LABEL)   += jump_label.o
 
diff --git a/arch/riscv/kernel/crash_save_regs.S 
b/arch/riscv/kernel/crash_save_regs.S
new file mode 100644
index 0..7832fb763
--- /dev/null
+++ b/arch/riscv/kernel/crash_save_regs.S
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2020 FORTH-ICS/CARV
+ *  Nick Kossifidis 
+ */
+
+#include/* For RISCV_* and REG_* macros */
+#include/* For CSR_* macros */
+#include/* For offsets on pt_regs */
+#include  /* For SYM_* macros */
+
+.section ".text"
+SYM_CODE_START(riscv_crash_save_regs)
+   REG_S ra,  PT_RA(a0)/* x1 */
+   REG_S sp,  PT_SP(a0)/* x2 */
+   REG_S gp,  PT_GP(a0)/* x3 */
+   REG_S tp,  PT_TP(a0)/* x4 */
+   REG_S t0,  PT_T0(a0)/* x5 */
+   REG_S t1,  PT_T1(a0)/* x6 */
+   REG_S t2,  PT_T2(a0)/* x7 */
+   REG_S s0,  PT_S0(a0)/* x8/fp */
+   REG_S s1,  PT_