[PATCH 2/2] LoongArch: kdump: Set up kernel image segment

2023-02-24 Thread Youling Tang
On LoongArch, we can use the same kernel image as 1st kernel when
[1] is merged, but we have to modify the entry point as well as
segments' addresses in the kernel's elf header (or pei format
vmlinux.efi) in order to load them into correct places.

[1]: 
https://lore.kernel.org/loongarch/1677150391-12838-1-git-send-email-tangyoul...@loongson.cn/T/#t

Signed-off-by: Youling Tang 
---
 kexec/arch/loongarch/crashdump-loongarch.c | 22 ++
 kexec/arch/loongarch/crashdump-loongarch.h |  1 +
 kexec/arch/loongarch/kexec-elf-loongarch.c |  8 
 kexec/arch/loongarch/kexec-loongarch.c |  3 ++-
 kexec/arch/loongarch/kexec-pei-loongarch.c |  7 +++
 5 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/kexec/arch/loongarch/crashdump-loongarch.c 
b/kexec/arch/loongarch/crashdump-loongarch.c
index aaf6cf3..81250e4 100644
--- a/kexec/arch/loongarch/crashdump-loongarch.c
+++ b/kexec/arch/loongarch/crashdump-loongarch.c
@@ -183,6 +183,28 @@ int load_crashdump_segments(struct kexec_info *info)
return 0;
 }
 
+/*
+ * e_entry and p_paddr are actually in virtual address space.
+ * Those values will be translated to physcal addresses by using
+ * virt_to_phys() in add_segment().
+ * So let's fix up those values for later use so the memory base will be
+ * correctly replaced with crash_reserved_mem[usablemem_rgns.size - 1].start.
+ */
+void fixup_elf_addrs(struct mem_ehdr *ehdr)
+{
+   struct mem_phdr *phdr;
+   int i;
+
+   ehdr->e_entry += crash_reserved_mem[usablemem_rgns.size - 1].start;
+
+   for (i = 0; i < ehdr->e_phnum; i++) {
+   phdr = &ehdr->e_phdr[i];
+   if (phdr->p_type != PT_LOAD)
+   continue;
+   phdr->p_paddr += crash_reserved_mem[usablemem_rgns.size - 
1].start;
+   }
+}
+
 int get_crash_kernel_load_range(uint64_t *start, uint64_t *end)
 {
if (!usablemem_rgns.size)
diff --git a/kexec/arch/loongarch/crashdump-loongarch.h 
b/kexec/arch/loongarch/crashdump-loongarch.h
index 3eb4e0a..25ff24b 100644
--- a/kexec/arch/loongarch/crashdump-loongarch.h
+++ b/kexec/arch/loongarch/crashdump-loongarch.h
@@ -8,6 +8,7 @@ extern struct memory_range elfcorehdr_mem;
 
 int load_crashdump_segments(struct kexec_info *info);
 int is_crashkernel_mem_reserved(void);
+void fixup_elf_addrs(struct mem_ehdr *ehdr);
 int get_crash_kernel_load_range(uint64_t *start, uint64_t *end);
 
 #define PAGE_OFFSET0x9000ULL
diff --git a/kexec/arch/loongarch/kexec-elf-loongarch.c 
b/kexec/arch/loongarch/kexec-elf-loongarch.c
index 2bf128f..45387ca 100644
--- a/kexec/arch/loongarch/kexec-elf-loongarch.c
+++ b/kexec/arch/loongarch/kexec-elf-loongarch.c
@@ -90,6 +90,14 @@ int elf_loongarch_load(int argc, char **argv, const char 
*kernel_buf,
}
}
 
+   /* load the kernel */
+   if (info->kexec_flags & KEXEC_ON_CRASH)
+   /*
+* offset addresses in elf header in order to load
+* vmlinux (elf_exec) into crash kernel's memory.
+*/
+   fixup_elf_addrs(&ehdr);
+
info->entry = (void *)virt_to_phys(ehdr.e_entry);
 
result = elf_exec_load(&ehdr, info);
diff --git a/kexec/arch/loongarch/kexec-loongarch.c 
b/kexec/arch/loongarch/kexec-loongarch.c
index 4c7361c..f47c998 100644
--- a/kexec/arch/loongarch/kexec-loongarch.c
+++ b/kexec/arch/loongarch/kexec-loongarch.c
@@ -253,7 +253,8 @@ unsigned long loongarch_locate_kernel_segment(struct 
kexec_info *info)
unsigned long hole_end;
 
hole = (crash_reserved_mem[usablemem_rgns.size - 1].start < 
mem_min ?
-   mem_min : 
crash_reserved_mem[usablemem_rgns.size - 1].start);
+   mem_min : 
crash_reserved_mem[usablemem_rgns.size - 1].start) +
+   loongarch_mem.text_offset;
hole = _ALIGN_UP(hole, MiB(1));
hole_end = hole + loongarch_mem.text_offset + 
loongarch_mem.image_size;
 
diff --git a/kexec/arch/loongarch/kexec-pei-loongarch.c 
b/kexec/arch/loongarch/kexec-pei-loongarch.c
index f86ac61..1a11103 100644
--- a/kexec/arch/loongarch/kexec-pei-loongarch.c
+++ b/kexec/arch/loongarch/kexec-pei-loongarch.c
@@ -66,6 +66,13 @@ int pei_loongarch_load(int argc, char **argv, const char 
*buf,
 
kernel_entry = virt_to_phys(loongarch_header_kernel_entry(header));
 
+   if (info->kexec_flags & KEXEC_ON_CRASH)
+   /*
+* offset addresses in order to load vmlinux.efi into
+* crash kernel's memory.
+*/
+   kernel_entry += crash_reserved_mem[usablemem_rgns.size - 
1].start;
+
dbgprintf("%s: kernel_segment: %016lx\n", __func__, kernel_segment);
dbgprintf("%s: kernel_entry:   %016lx\n", __func__, kernel_entry);
dbgprintf("%s: ima

[PATCH 1/2] kexec: Default __NR_kexec_file_load is set to undefined

2023-02-24 Thread Youling Tang
The initial reason is that after the merger of 29fe5067ed07
("kexec: make -a the default"), kexec cannot be used on LoongArch,
MIPS .etc architectures. We need to add "-c" for normal use. The
current kexec_file_load system call is not implemented in
architectures such as LoongArch, so it needs to pass kexec_load.
So we need to set __NR_kexec_file_load to undefined in unsupported
architectures. This will return EFALLBACK via is_kexec_file_load_implemented,
and then via kexec_load.

Signed-off-by: Youling Tang 
---
 kexec/kexec-syscall.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/kexec/kexec-syscall.h b/kexec/kexec-syscall.h
index be6ccd5..ea77936 100644
--- a/kexec/kexec-syscall.h
+++ b/kexec/kexec-syscall.h
@@ -59,9 +59,7 @@
 #endif
 #endif /*ifndef __NR_kexec_load*/
 
-#ifdef __arm__
 #undef __NR_kexec_file_load
-#endif
 
 #ifndef __NR_kexec_file_load
 
-- 
2.37.1


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH 0/5] Add LoongArch port support

2022-10-08 Thread Youling Tang

Hi, Simon

On 10/05/2022 11:19 PM, Simon Horman wrote:

On Wed, Sep 28, 2022 at 10:28:21AM +0800, Youling Tang wrote:

This patch series to support kexec/kdump (only 64bit).

Note: Kernel kexec/kdump support patch see link [1]:
Link [1]: 
https://lore.kernel.org/loongarch/711d53ae-197f-3c55-d09d-2f40e6886...@loongson.cn/T/#t


Thanks,

in principle this looks good to me.
But I am wondering if you could provide a patch to .github/workflows/build.yml


The LoongArch architecture is not yet integrated into ubuntu-20.04, and
it may not be possible to build LoongArch directly on that system via
build.yml.

Can we not add LoongArch to build.yml for now when it is not integrated
into ubuntu?

Thanks,
Youling.





___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH 3/5] LoongArch: PE format image loading support

2022-09-27 Thread Youling Tang
The LoongArch kernel will mainly use the vmlinux.efi image in PE format,
so add it support.

I tested this on LoongArch 3A5000 machine and works as expected,

kexec:
  $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
  $ sudo kexec -e

kdump:
  $ sudo kexec -p /boot/vmlinux-kdump.efi --reuse-cmdline --append="nr_cpus=1"
  # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 kexec/arch/loongarch/Makefile  |   2 +
 kexec/arch/loongarch/image-header.h|  79 ++
 kexec/arch/loongarch/kexec-elf-loongarch.c |   3 +
 kexec/arch/loongarch/kexec-loongarch.c |  19 
 kexec/arch/loongarch/kexec-loongarch.h |   9 ++
 kexec/arch/loongarch/kexec-pei-loongarch.c | 117 +
 6 files changed, 229 insertions(+)
 create mode 100644 kexec/arch/loongarch/image-header.h
 create mode 100644 kexec/arch/loongarch/kexec-pei-loongarch.c

diff --git a/kexec/arch/loongarch/Makefile b/kexec/arch/loongarch/Makefile
index e5e190a..3b33b96 100644
--- a/kexec/arch/loongarch/Makefile
+++ b/kexec/arch/loongarch/Makefile
@@ -3,6 +3,7 @@
 #
 loongarch_KEXEC_SRCS =  kexec/arch/loongarch/kexec-loongarch.c
 loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-elf-loongarch.c
+loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-pei-loongarch.c
 loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-elf-rel-loongarch.c
 loongarch_KEXEC_SRCS += kexec/arch/loongarch/crashdump-loongarch.c
 
@@ -16,5 +17,6 @@ loongarch_VIRT_TO_PHYS =
 
 dist += kexec/arch/loongarch/Makefile $(loongarch_KEXEC_SRCS)  
\
kexec/arch/loongarch/kexec-loongarch.h  
\
+   kexec/arch/loongarch/image-header.h 
\
kexec/arch/loongarch/crashdump-loongarch.h  
\
kexec/arch/loongarch/include/arch/options.h
diff --git a/kexec/arch/loongarch/image-header.h 
b/kexec/arch/loongarch/image-header.h
new file mode 100644
index 000..3b75765
--- /dev/null
+++ b/kexec/arch/loongarch/image-header.h
@@ -0,0 +1,79 @@
+/*
+ * LoongArch binary image header.
+ */
+
+#if !defined(__LOONGARCH_IMAGE_HEADER_H)
+#define __LOONGARCH_IMAGE_HEADER_H
+
+#include 
+#include 
+
+/**
+ * struct loongarch_image_header
+ *
+ * @pe_sig: Optional PE format 'MZ' signature.
+ * @reserved_1: Reserved.
+ * @kernel_entry: Kernel image entry pointer.
+ * @image_size: An estimated size of the memory image size in LSB byte order.
+ * @text_offset: The image load offset in LSB byte order.
+ * @reserved_2: Reserved.
+ * @reserved_3: Reserved.
+ * @pe_header: Optional offset to a PE format header.
+ **/
+
+struct loongarch_image_header {
+   uint8_t pe_sig[2];
+   uint16_t reserved_1[3];
+   uint64_t kernel_entry;
+   uint64_t image_size;
+   uint64_t text_offset;
+   uint64_t reserved_2[3];
+   uint32_t reserved_3;
+   uint32_t pe_header;
+};
+
+static const uint8_t loongarch_image_pe_sig[2] = {'M', 'Z'};
+
+/**
+ * loongarch_header_check_pe_sig - Helper to check the loongarch image header.
+ *
+ * Returns non-zero if 'MZ' signature is found.
+ */
+
+static inline int loongarch_header_check_pe_sig(const struct 
loongarch_image_header *h)
+{
+   if (!h)
+   return 0;
+
+   return (h->pe_sig[0] == loongarch_image_pe_sig[0]
+   && h->pe_sig[1] == loongarch_image_pe_sig[1]);
+}
+
+static inline uint64_t loongarch_header_text_offset(
+   const struct loongarch_image_header *h)
+{
+   if (!h)
+   return 0;
+
+   return le64toh(h->text_offset);
+}
+
+static inline uint64_t loongarch_header_image_size(
+   const struct loongarch_image_header *h)
+{
+   if (!h)
+   return 0;
+
+   return le64toh(h->image_size);
+}
+
+static inline uint64_t loongarch_header_kernel_entry(
+   const struct loongarch_image_header *h)
+{
+   if (!h)
+   return 0;
+
+   return le64toh(h->kernel_entry);
+}
+
+#endif
diff --git a/kexec/arch/loongarch/kexec-elf-loongarch.c 
b/kexec/arch/loongarch/kexec-elf-loongarch.c
index a5ec356..2bf128f 100644
--- a/kexec/arch/loongarch/kexec-elf-loongarch.c
+++ b/kexec/arch/loongarch/kexec-elf-loongarch.c
@@ -50,6 +50,7 @@ out:
 int elf_loongarch_load(int argc, char **argv, const char *kernel_buf,
off_t kernel_size, struct kexec_info *info)
 {
+   const struct loongarch_image_header *header = NULL;
unsigned long kernel_segment;
struct mem_ehdr ehdr;
int result;
@@ -76,6 +77,8 @@ int elf_loongarch_load(int argc, char **argv, const char 
*kernel_buf,
loongarch_mem.text_offset);
dbgprintf("%s: phys_offset:%016lx\n", __func__,
loongarch_mem.phys_offset);
+   dbgprintf("%s: PE format:  %s\n", __func__,
+   (loongarch_header_check_pe_sig(header) ? "yes" : "no"));
 
  

[PATCH 2/5] LoongArch: Add kexec/kdump support

2022-09-27 Thread Youling Tang
Add the 64-bit processing support of the LoongArch architecture. For the time
being, the quick restart function(kexec) is supported. That is, the "kexec -l"
and "kexec -e" commands can be used normally.

At the same time, the crash dump function also supports, "kexec -p" operation
can be successfully performed, and the vmcore file can be generated.

I tested this on  LoongArch 3A5000 machine and works as expected,

kexec:
  $ sudo kexec -l /boot/vmlinux --reuse-cmdline
  $ sudo kexec -e

kdump:
  $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
  # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 configure.ac  |   3 +
 include/elf.h |   1 +
 include/image.h   |   1 +
 kexec/Makefile|   1 +
 kexec/arch/loongarch/Makefile |  20 +
 kexec/arch/loongarch/crashdump-loongarch.c| 198 ++
 kexec/arch/loongarch/crashdump-loongarch.h|  25 ++
 kexec/arch/loongarch/include/arch/options.h   |  28 ++
 kexec/arch/loongarch/iomem.h  |  10 +
 kexec/arch/loongarch/kexec-elf-loongarch.c| 114 ++
 .../arch/loongarch/kexec-elf-rel-loongarch.c  |  42 +++
 kexec/arch/loongarch/kexec-loongarch.c| 353 ++
 kexec/arch/loongarch/kexec-loongarch.h|  51 +++
 kexec/kexec-syscall.h |   7 +
 14 files changed, 854 insertions(+)
 create mode 100644 kexec/arch/loongarch/Makefile
 create mode 100644 kexec/arch/loongarch/crashdump-loongarch.c
 create mode 100644 kexec/arch/loongarch/crashdump-loongarch.h
 create mode 100644 kexec/arch/loongarch/include/arch/options.h
 create mode 100644 kexec/arch/loongarch/iomem.h
 create mode 100644 kexec/arch/loongarch/kexec-elf-loongarch.c
 create mode 100644 kexec/arch/loongarch/kexec-elf-rel-loongarch.c
 create mode 100644 kexec/arch/loongarch/kexec-loongarch.c
 create mode 100644 kexec/arch/loongarch/kexec-loongarch.h

diff --git a/configure.ac b/configure.ac
index 0d825ef..819df6b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -58,6 +58,9 @@ case $target_cpu in
hppa*)
ARCH="hppa"
;;
+   loongarch*)
+   ARCH="loongarch"
+   ;;
* )
AC_MSG_ERROR([unsupported architecture $target_cpu])
;;
diff --git a/include/elf.h b/include/elf.h
index b7677a2..1c8d2cc 100644
--- a/include/elf.h
+++ b/include/elf.h
@@ -259,6 +259,7 @@ typedef struct
 #define EM_ARC_A5  93  /* ARC Cores Tangent-A5 */
 #define EM_XTENSA  94  /* Tensilica Xtensa Architecture */
 #define EM_AARCH64 183 /* ARM AARCH64 */
+#define EM_LOONGARCH   258 /* Loongson Loongarch*/
 #define EM_NUM 184
 
 /* If it is necessary to assign new unofficial EM_* values, please
diff --git a/include/image.h b/include/image.h
index 8e9d81e..7a4bccf 100644
--- a/include/image.h
+++ b/include/image.h
@@ -86,6 +86,7 @@
 #define IH_ARCH_ARC23  /* Synopsys DesignWare ARC */
 #define IH_ARCH_X86_64 24  /* AMD x86_64, Intel and Via */
 #define IH_ARCH_XTENSA 25  /* Xtensa   */
+#define IH_ARCH_LOONGARCH  26  /* LoongArch Loongson */
 
 /*
  * Image Types
diff --git a/kexec/Makefile b/kexec/Makefile
index e69e309..8a52e8d 100644
--- a/kexec/Makefile
+++ b/kexec/Makefile
@@ -92,6 +92,7 @@ include $(srcdir)/kexec/arch/s390/Makefile
 include $(srcdir)/kexec/arch/sh/Makefile
 include $(srcdir)/kexec/arch/x86_64/Makefile
 include $(srcdir)/kexec/arch/hppa/Makefile
+include $(srcdir)/kexec/arch/loongarch/Makefile
 
 KEXEC_SRCS += $($(ARCH)_KEXEC_SRCS)
 
diff --git a/kexec/arch/loongarch/Makefile b/kexec/arch/loongarch/Makefile
new file mode 100644
index 000..e5e190a
--- /dev/null
+++ b/kexec/arch/loongarch/Makefile
@@ -0,0 +1,20 @@
+#
+# kexec loongarch (linux booting linux)
+#
+loongarch_KEXEC_SRCS =  kexec/arch/loongarch/kexec-loongarch.c
+loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-elf-loongarch.c
+loongarch_KEXEC_SRCS += kexec/arch/loongarch/kexec-elf-rel-loongarch.c
+loongarch_KEXEC_SRCS += kexec/arch/loongarch/crashdump-loongarch.c
+
+loongarch_MEM_REGIONS = kexec/mem_regions.c
+
+loongarch_CPPFLAGS += -I $(srcdir)/kexec/
+
+loongarch_ADD_BUFFER =
+loongarch_ADD_SEGMENT =
+loongarch_VIRT_TO_PHYS =
+
+dist += kexec/arch/loongarch/Makefile $(loongarch_KEXEC_SRCS)  
\
+   kexec/arch/loongarch/kexec-loongarch.h  
\
+   kexec/arch/loongarch/crashdump-loongarch.h  
\
+   kexec/arch/loongarch/include/arch/options.h
diff --git a/kexec/arch/loongarch/crashdump-loongarch.c 
b/kexec/arch/loongarch/crashdump-loongarch.c
new file mode 100644
index 000..aaf6cf3
--- /dev/null
+++ b/kexec/arch/loongarch/crashdump-loongarch.c
@@ -

[PATCH 0/5] Add LoongArch port support

2022-09-27 Thread Youling Tang
This patch series to support kexec/kdump (only 64bit).

Note: Kernel kexec/kdump support patch see link [1]:
Link [1]: 
https://lore.kernel.org/loongarch/711d53ae-197f-3c55-d09d-2f40e6886...@loongson.cn/T/#t

Youling Tang (5):
  LoongArch: Add LoongArch architecture support in config.guess and
config.sub files
  LoongArch: Add kexec/kdump support
  LoongArch: PE format image loading support
  LoongArch: Add purgatory framework code
  LoongArch: Remove redundant cmdline parameters when using
--reuse-cmdline option

 config/config.guess   |   3 +
 config/config.sub |   1 +
 configure.ac  |   3 +
 include/elf.h |   1 +
 include/image.h   |   1 +
 kexec/Makefile|   1 +
 kexec/arch/loongarch/Makefile |  22 ++
 kexec/arch/loongarch/crashdump-loongarch.c| 198 ++
 kexec/arch/loongarch/crashdump-loongarch.h|  25 ++
 kexec/arch/loongarch/image-header.h   |  79 
 kexec/arch/loongarch/include/arch/options.h   |  28 ++
 kexec/arch/loongarch/iomem.h  |  10 +
 kexec/arch/loongarch/kexec-elf-loongarch.c| 117 ++
 .../arch/loongarch/kexec-elf-rel-loongarch.c  |  42 ++
 kexec/arch/loongarch/kexec-loongarch.c| 374 ++
 kexec/arch/loongarch/kexec-loongarch.h|  60 +++
 kexec/arch/loongarch/kexec-pei-loongarch.c| 117 ++
 kexec/kexec-syscall.h |   7 +
 kexec/kexec.c |   2 +-
 kexec/kexec.h |   1 +
 purgatory/Makefile|   1 +
 purgatory/arch/loongarch/Makefile |  10 +
 purgatory/arch/loongarch/console-loongarch.c  |   7 +
 .../arch/loongarch/purgatory-loongarch.c  |   7 +
 .../arch/loongarch/purgatory-loongarch.h  |   6 +
 25 files changed, 1122 insertions(+), 1 deletion(-)
 create mode 100644 kexec/arch/loongarch/Makefile
 create mode 100644 kexec/arch/loongarch/crashdump-loongarch.c
 create mode 100644 kexec/arch/loongarch/crashdump-loongarch.h
 create mode 100644 kexec/arch/loongarch/image-header.h
 create mode 100644 kexec/arch/loongarch/include/arch/options.h
 create mode 100644 kexec/arch/loongarch/iomem.h
 create mode 100644 kexec/arch/loongarch/kexec-elf-loongarch.c
 create mode 100644 kexec/arch/loongarch/kexec-elf-rel-loongarch.c
 create mode 100644 kexec/arch/loongarch/kexec-loongarch.c
 create mode 100644 kexec/arch/loongarch/kexec-loongarch.h
 create mode 100644 kexec/arch/loongarch/kexec-pei-loongarch.c
 create mode 100644 purgatory/arch/loongarch/Makefile
 create mode 100644 purgatory/arch/loongarch/console-loongarch.c
 create mode 100644 purgatory/arch/loongarch/purgatory-loongarch.c
 create mode 100644 purgatory/arch/loongarch/purgatory-loongarch.h

-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH 5/5] LoongArch: Remove redundant cmdline parameters when using --reuse-cmdline option

2022-09-27 Thread Youling Tang
In LoongArch, when using the --reuse-cmdline option to reuse the current
command line, it may lead to redundancy (like kexec, initrd command line
arguments). In order to avoid the possible impact of initrd removal on other
architectures, remove_parameter will be called in a specific architecture
for processing.

Signed-off-by: Youling Tang 
---
 kexec/arch/loongarch/kexec-loongarch.c | 2 ++
 kexec/kexec.c  | 2 +-
 kexec/kexec.h  | 1 +
 3 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/kexec/arch/loongarch/kexec-loongarch.c 
b/kexec/arch/loongarch/kexec-loongarch.c
index 3fdba01..4c7361c 100644
--- a/kexec/arch/loongarch/kexec-loongarch.c
+++ b/kexec/arch/loongarch/kexec-loongarch.c
@@ -219,6 +219,8 @@ int arch_process_options(int argc, char **argv)
break;
case OPT_REUSE_CMDLINE:
cmdline = get_command_line();
+   remove_parameter(cmdline, "kexec");
+   remove_parameter(cmdline, "initrd");
break;
case OPT_INITRD:
arch_options.initrd_file = optarg;
diff --git a/kexec/kexec.c b/kexec/kexec.c
index 829a6ea..0e92d96 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -1095,7 +1095,7 @@ static int k_status(unsigned long kexec_flags)
 /*
  * Remove parameter from a kernel command line. Helper function by 
get_command_line().
  */
-static void remove_parameter(char *line, const char *param_name)
+void remove_parameter(char *line, const char *param_name)
 {
char *start, *end;
 
diff --git a/kexec/kexec.h b/kexec/kexec.h
index 8a05644..0d820ad 100644
--- a/kexec/kexec.h
+++ b/kexec/kexec.h
@@ -304,6 +304,7 @@ int arch_compat_trampoline(struct kexec_info *info);
 void arch_update_purgatory(struct kexec_info *info);
 int is_crashkernel_mem_reserved(void);
 int get_crash_kernel_load_range(uint64_t *start, uint64_t *end);
+void remove_parameter(char *line, const char *param_name);
 char *get_command_line(void);
 
 int kexec_iomem_for_each_line(char *match,
-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH 4/5] LoongArch: Add purgatory framework code

2022-09-27 Thread Youling Tang
Add purgatory framework code, no specific implementation, just consistent
with other architectures.

Signed-off-by: Youling Tang 
---
 purgatory/Makefile |  1 +
 purgatory/arch/loongarch/Makefile  | 10 ++
 purgatory/arch/loongarch/console-loongarch.c   |  7 +++
 purgatory/arch/loongarch/purgatory-loongarch.c |  7 +++
 purgatory/arch/loongarch/purgatory-loongarch.h |  6 ++
 5 files changed, 31 insertions(+)
 create mode 100644 purgatory/arch/loongarch/Makefile
 create mode 100644 purgatory/arch/loongarch/console-loongarch.c
 create mode 100644 purgatory/arch/loongarch/purgatory-loongarch.c
 create mode 100644 purgatory/arch/loongarch/purgatory-loongarch.h

diff --git a/purgatory/Makefile b/purgatory/Makefile
index 15adb12..4d2d071 100644
--- a/purgatory/Makefile
+++ b/purgatory/Makefile
@@ -28,6 +28,7 @@ include $(srcdir)/purgatory/arch/ppc64/Makefile
 include $(srcdir)/purgatory/arch/s390/Makefile
 include $(srcdir)/purgatory/arch/sh/Makefile
 include $(srcdir)/purgatory/arch/x86_64/Makefile
+include $(srcdir)/purgatory/arch/loongarch/Makefile
 
 PURGATORY_SRCS+=$($(ARCH)_PURGATORY_SRCS)
 
diff --git a/purgatory/arch/loongarch/Makefile 
b/purgatory/arch/loongarch/Makefile
new file mode 100644
index 000..b0c47b2
--- /dev/null
+++ b/purgatory/arch/loongarch/Makefile
@@ -0,0 +1,10 @@
+#
+# Purgatory loongarch
+#
+
+loongarch_PURGATORY_SRCS+= purgatory/arch/loongarch/purgatory-loongarch.c
+loongarch_PURGATORY_SRCS+= purgatory/arch/loongarch/console-loongarch.c
+
+dist += purgatory/arch/loongarch/Makefile $(loongarch_PURGATORY_SRCS)  
\
+   purgatory/arch/loongarch/purgatory-loongarch.h
+
diff --git a/purgatory/arch/loongarch/console-loongarch.c 
b/purgatory/arch/loongarch/console-loongarch.c
new file mode 100644
index 000..af34ecf
--- /dev/null
+++ b/purgatory/arch/loongarch/console-loongarch.c
@@ -0,0 +1,7 @@
+#include 
+#include "unused.h"
+
+void putchar(int UNUSED(ch))
+{
+   /* Nothing for now */
+}
diff --git a/purgatory/arch/loongarch/purgatory-loongarch.c 
b/purgatory/arch/loongarch/purgatory-loongarch.c
new file mode 100644
index 000..abe9297
--- /dev/null
+++ b/purgatory/arch/loongarch/purgatory-loongarch.c
@@ -0,0 +1,7 @@
+#include 
+#include "purgatory-loongarch.h"
+
+void setup_arch(void)
+{
+   /* Nothing for now */
+}
diff --git a/purgatory/arch/loongarch/purgatory-loongarch.h 
b/purgatory/arch/loongarch/purgatory-loongarch.h
new file mode 100644
index 000..cd1ab97
--- /dev/null
+++ b/purgatory/arch/loongarch/purgatory-loongarch.h
@@ -0,0 +1,6 @@
+#ifndef PURGATORY_LOONGARCH_H
+#define PURGATORY_LOONGARCH_H
+
+/* nothing yet */
+
+#endif /* PURGATORY_LOONGARCH_H */
-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH 1/5] config: Add LoongArch architecture support in config.guess and config.sub files

2022-09-27 Thread Youling Tang
Add configuration files required by LoongArch architecture, including
config.guess and config.sub files.

The source file comes from:

https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess
  and
https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub

Signed-off-by: Youling Tang 
---
 config/config.guess | 3 +++
 config/config.sub   | 1 +
 2 files changed, 4 insertions(+)

diff --git a/config/config.guess b/config/config.guess
index 8d70ec2..c626f7a 100755
--- a/config/config.guess
+++ b/config/config.guess
@@ -983,6 +983,9 @@ EOF
 k1om:Linux:*:*)
echo "$UNAME_MACHINE"-unknown-linux-"$LIBC"
exit ;;
+loongarch32:Linux:*:* | loongarch64:Linux:*:* | loongarchx32:Linux:*:*)
+   echo "$UNAME_MACHINE"-unknown-linux-"$LIBC"
+   exit ;;
 m32r*:Linux:*:*)
echo "$UNAME_MACHINE"-unknown-linux-"$LIBC"
exit ;;
diff --git a/config/config.sub b/config/config.sub
index 9bc49a7..dae00e6 100755
--- a/config/config.sub
+++ b/config/config.sub
@@ -1185,6 +1185,7 @@ case $cpu-$vendor in
| k1om \
| le32 | le64 \
| lm32 \
+   | loongarch32 | loongarch64 | loongarchx32 \
| m32c | m32r | m32rle \
| m5200 | m68000 | m680[012346]0 | m68360 | m683?2 | 
m68k \
| m6811 | m68hc11 | m6812 | m68hc12 | m68hcs12x \
-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH v4 0/3] LoongArch: Add kexec/kdump support

2022-09-23 Thread Youling Tang




On 09/23/2022 03:44 PM, Youling Tang wrote:

This patch series to support kexec/kdump (only 64bit).

Kexec is a system call that enables you to load and boot into another kernel
from the currently running kernel. This is useful for kernel developers or
other people who need to reboot very quickly without waiting for the whole
BIOS boot process to finish.

Kdump uses kexec to quickly boot to a dump-capture kernel whenever a
dump of the system kernel's memory needs to be taken (for example, when
the system panics). The system kernel's memory image is preserved across
the reboot and is accessible to the dump-capture kernel.

For details, see Documentation/admin-guide/kdump/kdump.rst.

User tools kexec-tools see link [1].

TODO:
Currently kdump does not support the same binary image, the production kernel
and the capture kernel will be generated with different configurations. I will
support kernel relocation support in the near future. Then will go to implement
the same binary support based on kernel relocation support.

[1] Link: https://github.com/tangyouling/kexec-tools

Changes in v4:
 After applying the following series of patches [1], the following
 modifications need to be made:
 - Adjust the number of parameters and the content of the parameters passed.
 - Removed build and processing of fdt.
 - Add handling of command line segments.
 - Use the command line argument "elfcorehdr=" instead of the "linux,elfcorehdr"
   member in fdt.
 - Use the command line argument "mem=" instead of the "linux, 
usable-memory-range"
   member in fdt.
 - Use the command line argument "initrd=" instead of the "linux,initrd-start" 
and
   "linux,initrd-end" members of fdt.


For the command line "initrd=start,size" method:
We need to move the reserve_initrd_mem call after parse_early_param to
make sure we can get the correct start and size.

Youling.


 - Removed handling of "linux,elfcorehdr" and "linux, usable-memory-range".
 - Add handling of "elfcorehdr=".
 - Modify the handling of "mem=".
 - Add implementation of reserve_oldmem_region.
 - Reimplemented kexec-tools (note that kexec-tools needs to be updated).

   Link [1]: 
https://lore.kernel.org/loongarch/20220920183554.3870247-1-a...@kernel.org/T/#md02ad4a1510586b2df177cbce4422434eff2d457
   [PATCH v2 0/8] efi: disentangle the generic EFI stub from FDT

Changes in v3:
 - Adjust the PE header (note that kexec-tools needs to be updated).
 - Add ibar in kexec_reboot().
 - boot_flag is replaced by efi_boot.
 - Adjust do_kexec parameter passing order.
 - Adjust the order of static variables to be consistent with do_kexec.
 - Remove a-series register save.
 - Some comments and register usage modification.
 - Add the opening and closing of the cpu core state.
 - Add a call to cpu_device_up to turn it on when the cpu core state is offline.

Changes in v2:
 - Add ibar.
 - Access via IOCSR.
 - Remove the settings of the tp, sp registers.
 - Remove the crash.c file and merge the relevant code into machine_kexec.c.
 - Adjust the use of CONFIG_SMP macro to avoid build errors under !SMP
   configuration.
 - Change the default value of PHYSICAL_START of the capture kernel to
   0x9000a000.

Youling Tang (3):
  LoongArch: Add kexec support
  LoongArch: Add kdump support
  LoongArch: Enable CONFIG_KEXEC

 arch/loongarch/Kconfig |  33 +++
 arch/loongarch/Makefile|   4 +
 arch/loongarch/configs/loongson3_defconfig |   1 +
 arch/loongarch/include/asm/kexec.h |  60 
 arch/loongarch/kernel/Makefile |   3 +
 arch/loongarch/kernel/crash_dump.c |  19 ++
 arch/loongarch/kernel/head.S   |   6 +-
 arch/loongarch/kernel/machine_kexec.c  | 309 +
 arch/loongarch/kernel/mem.c|   1 +
 arch/loongarch/kernel/relocate_kernel.S| 112 
 arch/loongarch/kernel/setup.c  | 123 +++-
 arch/loongarch/kernel/traps.c  |   4 +
 12 files changed, 664 insertions(+), 11 deletions(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/crash_dump.c
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S




___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v4 3/3] LoongArch: Enable CONFIG_KEXEC

2022-09-23 Thread Youling Tang
Defaults enable CONFIG_KEXEC to convenient kexec operations.

Signed-off-by: Youling Tang 
---
 arch/loongarch/configs/loongson3_defconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/loongarch/configs/loongson3_defconfig 
b/arch/loongarch/configs/loongson3_defconfig
index f67127246aa8..c96ce646d84a 100644
--- a/arch/loongarch/configs/loongson3_defconfig
+++ b/arch/loongarch/configs/loongson3_defconfig
@@ -46,6 +46,7 @@ CONFIG_SMP=y
 CONFIG_HOTPLUG_CPU=y
 CONFIG_NR_CPUS=64
 CONFIG_NUMA=y
+CONFIG_KEXEC=y
 CONFIG_PAGE_SIZE_16KB=y
 CONFIG_HZ_250=y
 CONFIG_ACPI=y
-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v4 1/3] LoongArch: Add kexec support

2022-09-23 Thread Youling Tang
Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to
the LoongArch architecture, so as to add support for the kexec re-boot
mechanism (CONFIG_KEXEC) on LoongArch platforms.

Kexec supports loading vmlinux.elf in ELF format and vmlinux.efi in PE
format.

I tested kexec on LoongArch machines (Loongson-3A5000) and it works as
expected:

 $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
 $ sudo kexec -e

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  11 ++
 arch/loongarch/include/asm/kexec.h  |  60 +++
 arch/loongarch/kernel/Makefile  |   2 +
 arch/loongarch/kernel/head.S|   6 +-
 arch/loongarch/kernel/machine_kexec.c   | 221 
 arch/loongarch/kernel/relocate_kernel.S | 106 
 6 files changed, 405 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 221a3dbabfed..4352e9b271aa 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -419,6 +419,17 @@ config ARCH_IOREMAP
  protection support. However, you can enable LoongArch DMW-based
  ioremap() for better performance.
 
+config KEXEC
+   bool "Kexec system call"
+   select KEXEC_CORE
+   help
+ kexec is a system call that implements the ability to shutdown your
+ current kernel, and to start another kernel.  It is like a reboot
+ but it is independent of the system firmware.   And like a reboot
+ you can start any kernel with it, not just Linux.
+
+ The name comes from the similarity to the exec system call.
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h 
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..a4875952f0dd
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+   struct pt_regs *oldregs)
+{
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(*newregs));
+   else
+   prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+   unsigned long efi_boot;
+   unsigned long cmdline_ptr;
+   unsigned long efi_system_table;
+};
+
+typedef void (*do_kexec_t)(unsigned long efi_boot,
+  unsigned long cmdline_ptr,
+  unsigned long efi_system_table,
+  unsigned long jump_addr,
+  unsigned long first_ind_entry);
+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+extern void kexec_reboot(void);
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 7225916dd378..17554244db54 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
+obj-$(CONFIG_KEXEC)+= machine_kexec.o relocate_kernel.o
+
 obj-$(CONFIG_PROC_FS)  += proc.o
 
 obj-$(CONFIG_SMP)  += smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index e2074cd4fff4..4d352230fbc3 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,11 @@
 
 _head:
.word   MZ_MAGIC/* "MZ", MS-DOS header */
-   .org0x3c/* 0x04 ~ 0x3b reserved */
+   .org0x8
+   .dword  kernel_entry/* Kernel entry point */
+   .dword  _end - _text/* Kernel image effective size */
+   .quad   0   /* Kernel image load offset from start 
of RAM */
+   .org0x3c/* 0x20 ~ 

[PATCH v4 2/3] LoongArch: Add kdump support

2022-09-23 Thread Youling Tang
This patch adds support for kdump. In kdump case the normal kernel will
reserve a region for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash dump
file interface, /proc/vmcore, which can be viewed as a ELF file.

A user-space tool, such as kexec-tools, is responsible for allocating a
separate region for the core's ELF header within the crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to the crash dump kernel via a
command line argument "elfcorehdr=", and the crash dump kernel will
preserve this region for later use with loongarch_reserve_vmcore at
boot time.

At the same time, the crash kdump kernel is also limited within the
"crashkernel" area via a command line argument "mem=", so as not to
destroy the original kernel dump data.

In the crash dump kernel environment, /proc/vmcore is used to access the
primary kernel's memory with copy_oldmem_page().

I tested kdump on LoongArch machines (Loongson-3A5000) and it works as
expected (suggested crashkernel parameter is "crashkernel=512M@2560M"),
you may test it by triggering a crash through /proc/sysrq-trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq-trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  22 +
 arch/loongarch/Makefile |   4 +
 arch/loongarch/kernel/Makefile  |   1 +
 arch/loongarch/kernel/crash_dump.c  |  19 
 arch/loongarch/kernel/machine_kexec.c   |  98 ++-
 arch/loongarch/kernel/mem.c |   1 +
 arch/loongarch/kernel/relocate_kernel.S |   6 ++
 arch/loongarch/kernel/setup.c   | 123 ++--
 arch/loongarch/kernel/traps.c   |   4 +
 9 files changed, 263 insertions(+), 15 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 4352e9b271aa..b2da0a6f844e 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -430,6 +430,28 @@ config KEXEC
 
  The name comes from the similarity to the exec system call.
 
+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x9000a000"
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 42352f905858..ea17e692684e 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -69,7 +69,11 @@ endif
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)
 
+ifndef CONFIG_PHYSICAL_START
 load-y = 0x9020
+else
+load-y = $(CONFIG_PHYSICAL_START)
+endif
 bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y)
 
 drivers-$(CONFIG_PCI)  += arch/loongarch/pci/
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 17554244db54..53581442fa73 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_MODULES) += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
 obj-$(CONFIG_KEXEC)+= machine_kexec.o relocate_kernel.o
+obj-$(CONFIG_CRASH_DUMP)   += crash_dump.o
 
 obj-$(CONFIG_PROC_FS)  += proc.o
 
diff --git a/arch/loongarch/kernel/crash_dump.c 
b/arch/loongarch/kernel/crash_dump.c
new file mode 100644
index ..3a3711e57f39
--- /dev/null
+++ b/arch/loongarch/kernel/crash_dump.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include 
+#include 
+#include 
+
+ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
+size_t csize, unsigned long offset)
+{
+   void *vaddr;
+
+   if (!csize)
+   return 0;
+
+   vaddr = kmap_local_pfn(pfn);
+   csize = copy_to_iter(vaddr + offset, csize, iter);
+   kunmap_local(vaddr);
+
+   retu

[PATCH v4 0/3] LoongArch: Add kexec/kdump support

2022-09-23 Thread Youling Tang
This patch series to support kexec/kdump (only 64bit).

Kexec is a system call that enables you to load and boot into another kernel
from the currently running kernel. This is useful for kernel developers or
other people who need to reboot very quickly without waiting for the whole
BIOS boot process to finish.

Kdump uses kexec to quickly boot to a dump-capture kernel whenever a
dump of the system kernel's memory needs to be taken (for example, when
the system panics). The system kernel's memory image is preserved across
the reboot and is accessible to the dump-capture kernel.

For details, see Documentation/admin-guide/kdump/kdump.rst.

User tools kexec-tools see link [1].

TODO:
Currently kdump does not support the same binary image, the production kernel
and the capture kernel will be generated with different configurations. I will
support kernel relocation support in the near future. Then will go to implement
the same binary support based on kernel relocation support.

[1] Link: https://github.com/tangyouling/kexec-tools

Changes in v4:
 After applying the following series of patches [1], the following
 modifications need to be made:
 - Adjust the number of parameters and the content of the parameters passed.
 - Removed build and processing of fdt.
 - Add handling of command line segments.
 - Use the command line argument "elfcorehdr=" instead of the "linux,elfcorehdr"
   member in fdt.
 - Use the command line argument "mem=" instead of the "linux, 
usable-memory-range"
   member in fdt.
 - Use the command line argument "initrd=" instead of the "linux,initrd-start" 
and
   "linux,initrd-end" members of fdt.
 - Removed handling of "linux,elfcorehdr" and "linux, usable-memory-range".
 - Add handling of "elfcorehdr=".
 - Modify the handling of "mem=".
 - Add implementation of reserve_oldmem_region.
 - Reimplemented kexec-tools (note that kexec-tools needs to be updated).

   Link [1]: 
https://lore.kernel.org/loongarch/20220920183554.3870247-1-a...@kernel.org/T/#md02ad4a1510586b2df177cbce4422434eff2d457
   [PATCH v2 0/8] efi: disentangle the generic EFI stub from FDT

Changes in v3:
 - Adjust the PE header (note that kexec-tools needs to be updated).
 - Add ibar in kexec_reboot().
 - boot_flag is replaced by efi_boot.
 - Adjust do_kexec parameter passing order.
 - Adjust the order of static variables to be consistent with do_kexec.
 - Remove a-series register save.
 - Some comments and register usage modification.
 - Add the opening and closing of the cpu core state.
 - Add a call to cpu_device_up to turn it on when the cpu core state is offline.

Changes in v2:
 - Add ibar.
 - Access via IOCSR.
 - Remove the settings of the tp, sp registers.
 - Remove the crash.c file and merge the relevant code into machine_kexec.c.
 - Adjust the use of CONFIG_SMP macro to avoid build errors under !SMP
   configuration.
 - Change the default value of PHYSICAL_START of the capture kernel to
   0x9000a000.

Youling Tang (3):
  LoongArch: Add kexec support
  LoongArch: Add kdump support
  LoongArch: Enable CONFIG_KEXEC

 arch/loongarch/Kconfig |  33 +++
 arch/loongarch/Makefile|   4 +
 arch/loongarch/configs/loongson3_defconfig |   1 +
 arch/loongarch/include/asm/kexec.h |  60 
 arch/loongarch/kernel/Makefile |   3 +
 arch/loongarch/kernel/crash_dump.c |  19 ++
 arch/loongarch/kernel/head.S   |   6 +-
 arch/loongarch/kernel/machine_kexec.c  | 309 +
 arch/loongarch/kernel/mem.c|   1 +
 arch/loongarch/kernel/relocate_kernel.S| 112 
 arch/loongarch/kernel/setup.c  | 123 +++-
 arch/loongarch/kernel/traps.c  |   4 +
 12 files changed, 664 insertions(+), 11 deletions(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/crash_dump.c
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH v3 1/3] LoongArch: Add kexec support

2022-09-22 Thread Youling Tang

Hi, Ruoyao

On 09/22/2022 06:00 PM, Xi Ruoyao wrote:

On Thu, 2022-09-15 at 10:53 +0800, Youling Tang wrote:

+int machine_kexec_prepare(struct kimage *kimage)
+{
+   int i;
+   void *dtb = (void *)KEXEC_BLOB_ADDR;
+
+   kexec_image_info(kimage);
+
+   /* Find the Flattened Device Tree */
+   for (i = 0; i < kimage->nr_segments; i++) {
+   if (!fdt_check_header(kimage->segment[i].buf)) {


Hi Youling,

When I build Huacai's tree
(https://github.com/loongson/linux/commits/loongarch-next), there is a
strange error linking vmlinux:

ld: arch/loongarch/kernel/machine_kexec.o: in function `machine_kexec_prepare':
/home/xry111/git-repos/linux/arch/loongarch/kernel/machine_kexec.c:70: 
undefined reference to `fdt_check_header'

Not sure why...


This is because of the problem after efistub removes the fdt
modification, I will reimplement kexec/kdump. This build error can be
avoided by turning off CONFIG_KEXEC temporarily, tomorrow I will submit
a new kexec/kdump implementation.

Thanks,
Youling.





___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] makedumpfile: Add initial LoongArch64 support

2022-09-18 Thread Youling Tang
Patch adds support for LoongArch64 in makedumpfile. It takes care of
vmalloc, module and directly map kernel memory region's translation.
Currently we only support 3 leverl 16K pages and VA_BITS as 48.

The changes were tested on a LoongArch64 Loongson-3A5000 processor.
The dump compression and filtering (for all dump levels 1,2,4,8,16
and 31) tests are succussfull.

Signed-off-by: Youling Tang 
---
Note: kexec/kdump support patch see link [1]:
[1] Link: 
https://lore.kernel.org/loongarch/1663210426-15446-1-git-send-email-tangyoul...@loongson.cn/T/#t

 Makefile   |   2 +-
 arch/loongarch64.c | 113 +
 makedumpfile.h |  58 +++
 3 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch64.c

diff --git a/Makefile b/Makefile
index 370a97c..e07f466 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ endif
 SRC_BASE = makedumpfile.c makedumpfile.h diskdump_mod.h sadump_mod.h 
sadump_info.h
 SRC_PART = print_info.c dwarf_info.c elf_info.c erase_info.c sadump_info.c 
cache.c tools.c printk.c detect_cycle.c
 OBJ_PART=$(patsubst %.c,%.o,$(SRC_PART))
-SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c arch/ppc.c arch/sparc64.c arch/mips64.c
+SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c arch/ppc.c arch/sparc64.c arch/mips64.c 
arch/loongarch64.c
 OBJ_ARCH=$(patsubst %.c,%.o,$(SRC_ARCH))
 
 LIBS = -ldw -lbz2 -ldl -lelf -lz
diff --git a/arch/loongarch64.c b/arch/loongarch64.c
new file mode 100644
index 000..42a02ab
--- /dev/null
+++ b/arch/loongarch64.c
@@ -0,0 +1,113 @@
+/*
+ * loongarch64.c
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#ifdef __loongarch64__
+
+#include "../print_info.h"
+#include "../elf_info.h"
+#include "../makedumpfile.h"
+
+int
+get_phys_base_loongarch64(void)
+{
+   info->phys_base = 0ULL;
+
+   DEBUG_MSG("phys_base: %lx\n", info->phys_base);
+
+   return TRUE;
+}
+
+int
+get_machdep_info_loongarch64(void)
+{
+   info->section_size_bits = _SECTION_SIZE_BITS;
+
+   /* Check if we can get MAX_PHYSMEM_BITS from vmcoreinfo */
+   if (NUMBER(MAX_PHYSMEM_BITS) != NOT_FOUND_NUMBER)
+   info->max_physmem_bits = NUMBER(MAX_PHYSMEM_BITS);
+   else
+   info->max_physmem_bits = _MAX_PHYSMEM_BITS;
+
+   /* Check if we can get SECTION_SIZE_BITS from vmcoreinfo */
+   if (NUMBER(SECTION_SIZE_BITS) != NOT_FOUND_NUMBER)
+   info->section_size_bits = NUMBER(SECTION_SIZE_BITS);
+   else
+   info->section_size_bits = _SECTION_SIZE_BITS;
+
+   DEBUG_MSG("max_physmem_bits : %ld\n", info->max_physmem_bits);
+   DEBUG_MSG("section_size_bits: %ld\n", info->section_size_bits);
+
+   return TRUE;
+}
+
+int
+get_versiondep_info_loongarch64(void)
+{
+   info->page_offset  = _PAGE_OFFSET;
+
+   DEBUG_MSG("page_offset : %lx\n", info->page_offset);
+
+   return TRUE;
+}
+
+unsigned long long
+vaddr_to_paddr_loongarch64(unsigned long vaddr)
+{
+   unsigned long long paddr = NOT_PADDR;
+   pgd_t *pgda, pgdv;
+   pmd_t *pmda, pmdv;
+   pte_t *ptea, ptev;
+
+   if (vaddr >= _XKPRANGE && vaddr < _XKVRANGE)
+   return vaddr & ((1ULL << MAX_PHYSMEM_BITS()) - 1);
+
+   if (SYMBOL(swapper_pg_dir) == NOT_FOUND_SYMBOL) {
+   ERRMSG("Can't get the symbol of swapper_pg_dir.\n");
+   return NOT_PADDR;
+   }
+
+   pgda = pgd_offset(SYMBOL(swapper_pg_dir), vaddr);
+   if (!readmem(VADDR, (unsigned long long)pgda, &pgdv, sizeof(pgdv))) {
+   ERRMSG("Can't read pgd\n");
+   return NOT_PADDR;
+   }
+
+   pmda = pmd_offset(&pgdv, vaddr);
+   if (!readmem(VADDR, (unsigned long long)pmda, &pmdv, sizeof(pmdv))) {
+   ERRMSG("Can't read pmd\n");
+   return NOT_PADDR;
+   }
+
+   if (pmdv & _PAGE_HUGE) {
+   paddr = (pmdv & PMD_MASK) + (vaddr & (PMD_SIZE - 1));
+   return paddr;
+   }
+
+   ptea = pte_offset(&pmdv, vaddr);
+   if (!readmem(VADDR, (unsigned long long)ptea, &ptev, sizeof(ptev))) {
+   

[PATCH v3 2/3] LoongArch: Add kdump support

2022-09-14 Thread Youling Tang
This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2560M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  | 22 ++
 arch/loongarch/Makefile |  4 +
 arch/loongarch/kernel/Makefile  |  1 +
 arch/loongarch/kernel/crash_dump.c  | 19 +
 arch/loongarch/kernel/machine_kexec.c   | 98 +++--
 arch/loongarch/kernel/mem.c |  6 ++
 arch/loongarch/kernel/relocate_kernel.S |  6 ++
 arch/loongarch/kernel/setup.c   | 49 +
 arch/loongarch/kernel/traps.c   |  4 +
 9 files changed, 203 insertions(+), 6 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 08e063aaf847..4bf888c1 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -433,6 +433,28 @@ config KEXEC
 
  The name comes from the similarity to the exec system call.
 
+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x9000a000" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 69b39ba3a09d..224274c1644e 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -66,7 +66,11 @@ endif
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)
 
+ifdef CONFIG_PHYSICAL_START
+load-y = $(CONFIG_PHYSICAL_START)
+else
 load-y = 0x9020
+endif
 bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y)
 
 drivers-$(CONFIG_PCI)  += arch/loongarch/pci/
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 17dc8ce6b5ce..79eee7db1414 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_MODULES) += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
 obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+obj-$(CONFIG_CRASH_DUMP)+= crash_dump.o
 
 obj-$(CONFIG_PROC_FS)  += proc.o
 
diff --git a/arch/loongarch/kernel/crash_dump.c 
b/arch/loongarch/kernel/crash_dump.c
new file mode 100644
index ..13e5d2f7870d
--- /dev/null
+++ b/arch/loongarch/kernel/crash_dump.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include 
+#include 
+#include 
+
+ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
+size_t csize, unsigned long offset)
+{
+   void  *vaddr;
+
+   if (!csize)
+   return 0;
+
+   vaddr = kmap_local_pfn(pfn);
+   csize = copy_to_iter(vaddr + offset, csize, iter);
+   kunmap_local(vaddr);
+
+   return csize;
+}
diff --git a/arch/loongarch/kernel/machine_kex

[PATCH v3 1/3] LoongArch: Add kexec support

2022-09-14 Thread Youling Tang
Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to the
LoongArch architecture that add support for the kexec re-boot mechanis
(CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in
PE format.

I tested this on  LoongArch 3A5000 machine and works as expected,

 $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
 $ sudo kexec -e

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  11 ++
 arch/loongarch/include/asm/kexec.h  |  58 +++
 arch/loongarch/kernel/Makefile  |   2 +
 arch/loongarch/kernel/head.S|   6 +-
 arch/loongarch/kernel/machine_kexec.c   | 209 
 arch/loongarch/kernel/relocate_kernel.S | 105 
 6 files changed, 390 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 9b1f2ab878e9..08e063aaf847 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -422,6 +422,17 @@ config ARCH_IOREMAP
  protection support. However, you can enable LoongArch DMW-based
  ioremap() for better performance.
 
+config KEXEC
+   bool "Kexec system call"
+   select KEXEC_CORE
+   help
+ kexec is a system call that implements the ability to shutdown your
+ current kernel, and to start another kernel.  It is like a reboot
+ but it is independent of the system firmware.   And like a reboot
+ you can start any kernel with it, not just Linux.
+
+ The name comes from the similarity to the exec system call.
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h 
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..e5b022d335c7
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+   struct pt_regs *oldregs)
+{
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(*newregs));
+   else
+   prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+   unsigned long efi_boot;
+   unsigned long fdt_addr;
+};
+
+typedef void (*do_kexec_t)(unsigned long efi_boot,
+  unsigned long fdt_addr,
+  unsigned long jump_addr,
+  unsigned long first_ind_entry);
+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+extern void kexec_reboot(void);
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 7225916dd378..17dc8ce6b5ce 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
+obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+
 obj-$(CONFIG_PROC_FS)  += proc.o
 
 obj-$(CONFIG_SMP)  += smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index eb3f641d5915..c0d61b29fcaa 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,11 @@
 
 _head:
.word   MZ_MAGIC/* "MZ", MS-DOS header */
-   .org0x3c/* 0x04 ~ 0x3b reserved */
+   .org0x8
+   .dword  kernel_entry/* Kernel entry point */
+   .dword  _end - _text/* Effective size of kernel image */
+   .quad   0   /* Image load offset from start of RAM 
*/
+   .org0x3c/* 0x20 ~ 0x3b reserved */
.long   pe_header - _head   /* Offset to the PE header */
 
 pe_header:
diff --git a/arch/loong

[PATCH v3 3/3] LoongArch: Enable CONFIG_KEXEC

2022-09-14 Thread Youling Tang
Defaults enable CONFIG_KEXEC to convenient kexec operations.

Signed-off-by: Youling Tang 
---
 arch/loongarch/configs/loongson3_defconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/loongarch/configs/loongson3_defconfig 
b/arch/loongarch/configs/loongson3_defconfig
index 573f7a41f735..1ae85e797858 100644
--- a/arch/loongarch/configs/loongson3_defconfig
+++ b/arch/loongarch/configs/loongson3_defconfig
@@ -46,6 +46,7 @@ CONFIG_SMP=y
 CONFIG_HOTPLUG_CPU=y
 CONFIG_NR_CPUS=64
 CONFIG_NUMA=y
+CONFIG_KEXEC=y
 CONFIG_PAGE_SIZE_16KB=y
 CONFIG_HZ_250=y
 CONFIG_ACPI=y
-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v3 0/3] LoongArch: Add kexec/kdump support

2022-09-14 Thread Youling Tang
This patch series to support kexec/kdump (only 64bit).

Kexec is a system call that enables you to load and boot into another kernel
from the currently running kernel. This is useful for kernel developers or
other people who need to reboot very quickly without waiting for the whole
BIOS boot process to finish.

Kdump uses kexec to quickly boot to a dump-capture kernel whenever a
dump of the system kernel's memory needs to be taken (for example, when
the system panics). The system kernel's memory image is preserved across
the reboot and is accessible to the dump-capture kernel.

For details, see Documentation/admin-guide/kdump/kdump.rst.

User tools kexec-tools see link [1].

TODO:
Currently kdump does not support the same binary image, the production kernel
and the capture kernel will be generated with different configurations. I will
support kernel relocation support in the near future. Then will go to implement
the same binary support based on kernel relocation support.

[1] Link: https://github.com/tangyouling/kexec-tools

Changes in v3:
 - Adjust the PE header (note that kexec-tools needs to be updated).
 - Add ibar in kexec_reboot().
 - boot_flag is replaced by efi_boot.
 - Adjust do_kexec parameter passing order.
 - Adjust the order of static variables to be consistent with do_kexec.
 - Remove a-series register save.
 - Some comments and register usage modification.
 - Add the opening and closing of the cpu core state.
 - Add a call to cpu_device_up to turn it on when the cpu core state is offline.

Changes in v2:
 - Add ibar.
 - Access via IOCSR.
 - Remove the settings of the tp, sp registers.
 - Remove the crash.c file and merge the relevant code into machine_kexec.c.
 - Adjust the use of CONFIG_SMP macro to avoid build errors under !SMP
   configuration.
 - Change the default value of PHYSICAL_START of the capture kernel to
   0x9000a000.

Youling Tang (3):
  LoongArch: Add kexec support
  LoongArch: Add kdump support
  LoongArch: Enable CONFIG_KEXEC

 arch/loongarch/Kconfig |  33 +++
 arch/loongarch/Makefile|   4 +
 arch/loongarch/configs/loongson3_defconfig |   1 +
 arch/loongarch/include/asm/kexec.h |  58 
 arch/loongarch/kernel/Makefile |   3 +
 arch/loongarch/kernel/crash_dump.c |  19 ++
 arch/loongarch/kernel/head.S   |   6 +-
 arch/loongarch/kernel/machine_kexec.c  | 295 +
 arch/loongarch/kernel/mem.c|   6 +
 arch/loongarch/kernel/relocate_kernel.S| 111 
 arch/loongarch/kernel/setup.c  |  49 
 arch/loongarch/kernel/traps.c  |   4 +
 12 files changed, 588 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/crash_dump.c
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH v2 1/3] LoongArch: Add kexec support

2022-09-09 Thread Youling Tang

Hi, Huacai

On 09/09/2022 04:16 PM, Huacai Chen wrote:

Hi, Youling,

On Fri, Sep 9, 2022 at 11:20 AM Youling Tang  wrote:


Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to the
LoongArch architecture that add support for the kexec re-boot mechanis
(CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in
PE format.

I tested this on  LoongArch 3A5000 machine and works as expected,

 $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
 $ sudo kexec -e

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  11 ++
 arch/loongarch/include/asm/kexec.h  |  58 
 arch/loongarch/kernel/Makefile  |   2 +
 arch/loongarch/kernel/head.S|   7 +-
 arch/loongarch/kernel/machine_kexec.c   | 188 
 arch/loongarch/kernel/relocate_kernel.S | 106 +
 6 files changed, 371 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 9b1f2ab878e9..08e063aaf847 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -422,6 +422,17 @@ config ARCH_IOREMAP
  protection support. However, you can enable LoongArch DMW-based
  ioremap() for better performance.

+config KEXEC
+   bool "Kexec system call"
+   select KEXEC_CORE
+   help
+ kexec is a system call that implements the ability to shutdown your
+ current kernel, and to start another kernel.  It is like a reboot
+ but it is independent of the system firmware.   And like a reboot
+ you can start any kernel with it, not just Linux.
+
+ The name comes from the similarity to the exec system call.
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h 
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..f23506725e00
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+   struct pt_regs *oldregs)
+{
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(*newregs));
+   else
+   prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+   unsigned long boot_flag;
+   unsigned long fdt_addr;
+};

I prefer to change boot_flag to efi_boot, the latter is better to
correspond the current usage, and keeps consistency with efistub.



OK.

+
+typedef void (*do_kexec_t)(unsigned long boot_flag,
+  unsigned long fdt_addr,
+  unsigned long first_ind_entry,
+  unsigned long jump_addr);

I prefer change the order of jump_addr and first_ind_entry here.


OK.



+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+extern void kexec_reboot(void);
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 7225916dd378..17dc8ce6b5ce 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o

+obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+
 obj-$(CONFIG_PROC_FS)  += proc.o

 obj-$(CONFIG_SMP)  += smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index eb3f641d5915..0f786d670e66 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,12 @@

 _head:
.word   MZ_MAGIC/* "MZ", MS-DOS header */
-   .org0x3c/* 0x04 ~ 0x3b reserved */
+   .org0x8
+   .quad   0   /* Image

Re: [PATCH v2 2/3] LoongArch: Add kdump support

2022-09-09 Thread Youling Tang

Hi,Huacai

On 09/09/2022 04:15 PM, Huacai Chen wrote:

Hi, Youling,

On Fri, Sep 9, 2022 at 11:20 AM Youling Tang  wrote:


This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2560M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  | 22 ++
 arch/loongarch/Makefile |  4 ++
 arch/loongarch/kernel/Makefile  |  1 +
 arch/loongarch/kernel/crash_dump.c  | 19 +
 arch/loongarch/kernel/machine_kexec.c   | 95 +++--
 arch/loongarch/kernel/mem.c |  6 ++
 arch/loongarch/kernel/relocate_kernel.S |  6 ++
 arch/loongarch/kernel/setup.c   | 49 +
 arch/loongarch/kernel/traps.c   |  4 ++
 9 files changed, 200 insertions(+), 6 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 08e063aaf847..4bf888c1 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -433,6 +433,28 @@ config KEXEC

  The name comes from the similarity to the exec system call.

+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x9000a000" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 69b39ba3a09d..224274c1644e 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -66,7 +66,11 @@ endif
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)

+ifdef CONFIG_PHYSICAL_START
+load-y = $(CONFIG_PHYSICAL_START)
+else
 load-y = 0x9020
+endif
 bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y)

 drivers-$(CONFIG_PCI)  += arch/loongarch/pci/
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 17dc8ce6b5ce..79eee7db1414 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_MODULES) += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o

 obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+obj-$(CONFIG_CRASH_DUMP)+= crash_dump.o

 obj-$(CONFIG_PROC_FS)  += proc.o

diff --git a/arch/loongarch/kernel/crash_dump.c 
b/arch/loongarch/kernel/crash_dump.c
new file mode 100644
index ..13e5d2f7870d
--- /dev/null
+++ b/arch/loongarch/kernel/crash_dump.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include 
+#include 
+#include 
+
+ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
+size_t csize, unsigned long offset)
+{
+   void  *vaddr;
+
+   if (!csize)
+   return 0;
+
+   vaddr = kmap_local_pfn(pfn);
+   csize = copy_to_iter(vaddr +

[PATCH v2 2/3] LoongArch: Add kdump support

2022-09-08 Thread Youling Tang
This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2560M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  | 22 ++
 arch/loongarch/Makefile |  4 ++
 arch/loongarch/kernel/Makefile  |  1 +
 arch/loongarch/kernel/crash_dump.c  | 19 +
 arch/loongarch/kernel/machine_kexec.c   | 95 +++--
 arch/loongarch/kernel/mem.c |  6 ++
 arch/loongarch/kernel/relocate_kernel.S |  6 ++
 arch/loongarch/kernel/setup.c   | 49 +
 arch/loongarch/kernel/traps.c   |  4 ++
 9 files changed, 200 insertions(+), 6 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 08e063aaf847..4bf888c1 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -433,6 +433,28 @@ config KEXEC
 
  The name comes from the similarity to the exec system call.
 
+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x9000a000" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 69b39ba3a09d..224274c1644e 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -66,7 +66,11 @@ endif
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)
 
+ifdef CONFIG_PHYSICAL_START
+load-y = $(CONFIG_PHYSICAL_START)
+else
 load-y = 0x9020
+endif
 bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y)
 
 drivers-$(CONFIG_PCI)  += arch/loongarch/pci/
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 17dc8ce6b5ce..79eee7db1414 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -18,6 +18,7 @@ obj-$(CONFIG_MODULES) += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
 obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+obj-$(CONFIG_CRASH_DUMP)+= crash_dump.o
 
 obj-$(CONFIG_PROC_FS)  += proc.o
 
diff --git a/arch/loongarch/kernel/crash_dump.c 
b/arch/loongarch/kernel/crash_dump.c
new file mode 100644
index ..13e5d2f7870d
--- /dev/null
+++ b/arch/loongarch/kernel/crash_dump.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+#include 
+#include 
+#include 
+
+ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
+size_t csize, unsigned long offset)
+{
+   void  *vaddr;
+
+   if (!csize)
+   return 0;
+
+   vaddr = kmap_local_pfn(pfn);
+   csize = copy_to_iter(vaddr + offset, csize, iter);
+   kunmap_local(vaddr);
+
+   return csize;
+}
diff --git a/arch/loongarch/kernel/mach

[PATCH v2 0/3] LoongArch: Add kexec/kdump support

2022-09-08 Thread Youling Tang
This patch series to support kexec/kdump (only 64bit).

Kexec is a system call that enables you to load and boot into another kernel
from the currently running kernel. This is useful for kernel developers or
other people who need to reboot very quickly without waiting for the whole
BIOS boot process to finish. 

Kdump uses kexec to quickly boot to a dump-capture kernel whenever a
dump of the system kernel's memory needs to be taken (for example, when
the system panics). The system kernel's memory image is preserved across
the reboot and is accessible to the dump-capture kernel.

For details, see Documentation/admin-guide/kdump/kdump.rst.

User tools kexec-tools see link [1].

TODO:
Currently kdump does not support the same binary image, the production kernel
and the capture kernel will be generated with different configurations. I will
support kernel relocation support in the near future. Then will go to implement
the same binary support based on kernel relocation support.

[1] Link: https://github.com/tangyouling/kexec-tools

Changes in v2:
 - Add ibar.
 - Access via IOCSR.
 - Remove the settings of the tp, sp registers.
 - Remove the crash.c file and merge the relevant code into machine_kexec.c.
 - Adjust the use of CONFIG_SMP macro to avoid build errors under !SMP
   configuration.
 - Change the default value of PHYSICAL_START of the capture kernel to
   0x9000a000.

Youling Tang (3):
  LoongArch: Add kexec support
  LoongArch: Add kdump support
  LoongArch: Enable CONFIG_KEXEC

 arch/loongarch/Kconfig |  33 +++
 arch/loongarch/Makefile|   4 +
 arch/loongarch/configs/loongson3_defconfig |   1 +
 arch/loongarch/include/asm/kexec.h |  58 +
 arch/loongarch/kernel/Makefile |   3 +
 arch/loongarch/kernel/crash_dump.c |  19 ++
 arch/loongarch/kernel/head.S   |   7 +-
 arch/loongarch/kernel/machine_kexec.c  | 271 +
 arch/loongarch/kernel/mem.c|   6 +
 arch/loongarch/kernel/relocate_kernel.S| 112 +
 arch/loongarch/kernel/setup.c  |  49 
 arch/loongarch/kernel/traps.c  |   4 +
 12 files changed, 566 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/crash_dump.c
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2 3/3] LoongArch: Enable CONFIG_KEXEC

2022-09-08 Thread Youling Tang
Defaults enable CONFIG_KEXEC to convenient kexec operations.

Signed-off-by: Youling Tang 
---
 arch/loongarch/configs/loongson3_defconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/loongarch/configs/loongson3_defconfig 
b/arch/loongarch/configs/loongson3_defconfig
index 573f7a41f735..1ae85e797858 100644
--- a/arch/loongarch/configs/loongson3_defconfig
+++ b/arch/loongarch/configs/loongson3_defconfig
@@ -46,6 +46,7 @@ CONFIG_SMP=y
 CONFIG_HOTPLUG_CPU=y
 CONFIG_NR_CPUS=64
 CONFIG_NUMA=y
+CONFIG_KEXEC=y
 CONFIG_PAGE_SIZE_16KB=y
 CONFIG_HZ_250=y
 CONFIG_ACPI=y
-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2 1/3] LoongArch: Add kexec support

2022-09-08 Thread Youling Tang
Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to the
LoongArch architecture that add support for the kexec re-boot mechanis
(CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in
PE format.

I tested this on  LoongArch 3A5000 machine and works as expected,

 $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
 $ sudo kexec -e

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  11 ++
 arch/loongarch/include/asm/kexec.h  |  58 
 arch/loongarch/kernel/Makefile  |   2 +
 arch/loongarch/kernel/head.S|   7 +-
 arch/loongarch/kernel/machine_kexec.c   | 188 
 arch/loongarch/kernel/relocate_kernel.S | 106 +
 6 files changed, 371 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 9b1f2ab878e9..08e063aaf847 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -422,6 +422,17 @@ config ARCH_IOREMAP
  protection support. However, you can enable LoongArch DMW-based
  ioremap() for better performance.
 
+config KEXEC
+   bool "Kexec system call"
+   select KEXEC_CORE
+   help
+ kexec is a system call that implements the ability to shutdown your
+ current kernel, and to start another kernel.  It is like a reboot
+ but it is independent of the system firmware.   And like a reboot
+ you can start any kernel with it, not just Linux.
+
+ The name comes from the similarity to the exec system call.
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h 
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..f23506725e00
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+   struct pt_regs *oldregs)
+{
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(*newregs));
+   else
+   prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+   unsigned long boot_flag;
+   unsigned long fdt_addr;
+};
+
+typedef void (*do_kexec_t)(unsigned long boot_flag,
+  unsigned long fdt_addr,
+  unsigned long first_ind_entry,
+  unsigned long jump_addr);
+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+extern void kexec_reboot(void);
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 7225916dd378..17dc8ce6b5ce 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
+obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+
 obj-$(CONFIG_PROC_FS)  += proc.o
 
 obj-$(CONFIG_SMP)  += smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index eb3f641d5915..0f786d670e66 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,12 @@
 
 _head:
.word   MZ_MAGIC/* "MZ", MS-DOS header */
-   .org0x3c/* 0x04 ~ 0x3b reserved */
+   .org0x8
+   .quad   0   /* Image load offset from start of RAM 
*/
+   .dword  _end - _text/* Effective size of kernel image */
+   .quad   0
+   .dword  kernel_entry/* Kernel entry point */
+   .org0x3c/* 0x28 ~ 0x3b reserved */
.long   pe_header - _head   /* Offset to the PE header */
 
 pe_heade

Re: [PATCH 2/3] LoongArch: Add kdump support

2022-09-05 Thread Youling Tang

Hi, Huacai

On 09/05/2022 03:32 PM, Huacai Chen wrote:

Hi, Youling,

On Mon, Sep 5, 2022 at 10:22 AM Youling Tang  wrote:




On 09/05/2022 10:14 AM, Huacai Chen wrote:

On Mon, Sep 5, 2022 at 10:04 AM Youling Tang  wrote:


Hi, Huacai

On 09/05/2022 09:38 AM, Huacai Chen wrote:

Hi, Youling,

On Mon, Sep 5, 2022 at 8:54 AM Youling Tang  wrote:


Hi, Huacai

On 09/04/2022 08:21 PM, Huacai Chen wrote:

Hi, Youling,

I think crash.c can be merged into crash_dump.c


Most architectures only implement copy_oldmem_page() in crash_dump.c,
I'm not sure if merging crash.c into crash_dump.c will break its
consistency?

Thanks,
Youling

Yes, you are right, crash.c cannot be merged into crash_dump.c, but it
can be merged into machine_kexec.c, as arm64 and riscv do.


For arm64, machine_crash_shutdown() is placed in machine_kexec.c, and
crash_smp_send_stop is placed in smp.c. If crash.c needs to be merged
into machine_kexec.c, should crash_shutdown_secondary and
crash_smp_send_stop be placed in smp.c?

I don't want to touch smp.c, all merged into machine_kexec.c seems reasonable.


Ok, I'll merge all into machine_kexec.c.

Youling.

Another problem, 0x90009100 for PHYSICAL_START is too tricky.
If you want to skip the "low memory", maybe we can use
0x90009000 or 0x9000a000?


Because there are many holes in our memory layout, if PHYSICAL_START is
set to 0x9000a000, the largest reserved area of ​​the crashkernel
will be 512M, beyond which it will fail.

# cat /proc/iomem
9040-bfff : System RAM
c002-f9ef : System RAM
  f681-f6813fff : Reserved

The second System RAM starts at 0x9040, so 0x90009000 will
be too small.

Youling.



Huacai




Huacai


Youling.


Huacai




Huacai

On Mon, Aug 29, 2022 at 12:37 PM Youling Tang  wrote:


This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2320M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  22 ++
 arch/loongarch/Makefile |   4 +
 arch/loongarch/kernel/Makefile  |   3 +-
 arch/loongarch/kernel/crash.c   | 100 
 arch/loongarch/kernel/crash_dump.c  |  19 +
 arch/loongarch/kernel/machine_kexec.c   |  12 ++-
 arch/loongarch/kernel/mem.c |   6 ++
 arch/loongarch/kernel/relocate_kernel.S |   6 ++
 arch/loongarch/kernel/setup.c   |  49 
 arch/loongarch/kernel/traps.c   |   4 +
 10 files changed, 217 insertions(+), 8 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash.c
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 903c82fa958d..7c1b07a5b5bd 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -420,6 +420,28 @@ config KEXEC

  The name comes from the similarity to the exec system call.

+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x90009100" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the

Re: [PATCH 2/3] LoongArch: Add kdump support

2022-09-05 Thread Youling Tang

Hi, Huacai

On 09/05/2022 09:01 PM, Huacai Chen wrote:

Hi, Youling,

On Mon, Sep 5, 2022 at 3:45 PM Youling Tang  wrote:


Hi, Huacai

On 09/05/2022 03:32 PM, Huacai Chen wrote:

Hi, Youling,

On Mon, Sep 5, 2022 at 10:22 AM Youling Tang  wrote:




On 09/05/2022 10:14 AM, Huacai Chen wrote:

On Mon, Sep 5, 2022 at 10:04 AM Youling Tang  wrote:


Hi, Huacai

On 09/05/2022 09:38 AM, Huacai Chen wrote:

Hi, Youling,

On Mon, Sep 5, 2022 at 8:54 AM Youling Tang  wrote:


Hi, Huacai

On 09/04/2022 08:21 PM, Huacai Chen wrote:

Hi, Youling,

I think crash.c can be merged into crash_dump.c


Most architectures only implement copy_oldmem_page() in crash_dump.c,
I'm not sure if merging crash.c into crash_dump.c will break its
consistency?

Thanks,
Youling

Yes, you are right, crash.c cannot be merged into crash_dump.c, but it
can be merged into machine_kexec.c, as arm64 and riscv do.


For arm64, machine_crash_shutdown() is placed in machine_kexec.c, and
crash_smp_send_stop is placed in smp.c. If crash.c needs to be merged
into machine_kexec.c, should crash_shutdown_secondary and
crash_smp_send_stop be placed in smp.c?

I don't want to touch smp.c, all merged into machine_kexec.c seems reasonable.


Ok, I'll merge all into machine_kexec.c.

Youling.

Another problem, 0x90009100 for PHYSICAL_START is too tricky.
If you want to skip the "low memory", maybe we can use
0x90009000 or 0x9000a000?


Because there are many holes in our memory layout, if PHYSICAL_START is
set to 0x9000a000, the largest reserved area of the crashkernel
will be 512M, beyond which it will fail.

Then 0x90009000 is not suitable, but I think 512M is enough?
If so, let's use 0x9000a000.


I'm not sure if it's enough for the server machine?
I will change to 0x9000a000.

Youling.


Huacai

Huacai


# cat /proc/iomem
9040-bfff : System RAM
c002-f9ef : System RAM
   f681-f6813fff : Reserved

The second System RAM starts at 0x9040, so 0x90009000 will
be too small.

Youling.



Huacai




Huacai


Youling.


Huacai




Huacai

On Mon, Aug 29, 2022 at 12:37 PM Youling Tang  wrote:


This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2320M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  22 ++
 arch/loongarch/Makefile |   4 +
 arch/loongarch/kernel/Makefile  |   3 +-
 arch/loongarch/kernel/crash.c   | 100 
 arch/loongarch/kernel/crash_dump.c  |  19 +
 arch/loongarch/kernel/machine_kexec.c   |  12 ++-
 arch/loongarch/kernel/mem.c |   6 ++
 arch/loongarch/kernel/relocate_kernel.S |   6 ++
 arch/loongarch/kernel/setup.c   |  49 
 arch/loongarch/kernel/traps.c   |   4 +
 10 files changed, 217 insertions(+), 8 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash.c
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 903c82fa958d..7c1b07a5b5bd 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -420,6 +420,28 @@ config KEXEC

  The name comes from the similarity to the exec system call.

+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START

Re: [PATCH 2/3] LoongArch: Add kdump support

2022-09-04 Thread Youling Tang




On 09/05/2022 10:14 AM, Huacai Chen wrote:

On Mon, Sep 5, 2022 at 10:04 AM Youling Tang  wrote:


Hi, Huacai

On 09/05/2022 09:38 AM, Huacai Chen wrote:

Hi, Youling,

On Mon, Sep 5, 2022 at 8:54 AM Youling Tang  wrote:


Hi, Huacai

On 09/04/2022 08:21 PM, Huacai Chen wrote:

Hi, Youling,

I think crash.c can be merged into crash_dump.c


Most architectures only implement copy_oldmem_page() in crash_dump.c,
I'm not sure if merging crash.c into crash_dump.c will break its
consistency?

Thanks,
Youling

Yes, you are right, crash.c cannot be merged into crash_dump.c, but it
can be merged into machine_kexec.c, as arm64 and riscv do.


For arm64, machine_crash_shutdown() is placed in machine_kexec.c, and
crash_smp_send_stop is placed in smp.c. If crash.c needs to be merged
into machine_kexec.c, should crash_shutdown_secondary and
crash_smp_send_stop be placed in smp.c?

I don't want to touch smp.c, all merged into machine_kexec.c seems reasonable.


Ok, I'll merge all into machine_kexec.c.

Youling.



Huacai


Youling.


Huacai




Huacai

On Mon, Aug 29, 2022 at 12:37 PM Youling Tang  wrote:


This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2320M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  22 ++
 arch/loongarch/Makefile |   4 +
 arch/loongarch/kernel/Makefile  |   3 +-
 arch/loongarch/kernel/crash.c   | 100 
 arch/loongarch/kernel/crash_dump.c  |  19 +
 arch/loongarch/kernel/machine_kexec.c   |  12 ++-
 arch/loongarch/kernel/mem.c |   6 ++
 arch/loongarch/kernel/relocate_kernel.S |   6 ++
 arch/loongarch/kernel/setup.c   |  49 
 arch/loongarch/kernel/traps.c   |   4 +
 10 files changed, 217 insertions(+), 8 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash.c
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 903c82fa958d..7c1b07a5b5bd 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -420,6 +420,28 @@ config KEXEC

  The name comes from the similarity to the exec system call.

+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x90009100" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 4bc47f47cfd8..7dabd580426d 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -48,7 +48,11 @@ KBUILD_CFLAGS_MODULE += -fplt 
-Wa,-mla-global-with-abs,-mla-local-with-abs
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)

+ifdef CONFIG_PHYSICAL_START
+load-y = $(CONFIG_PHYSICAL_START)
+else
 load

Re: [PATCH 2/3] LoongArch: Add kdump support

2022-09-04 Thread Youling Tang

Hi, Huacai

On 09/05/2022 09:38 AM, Huacai Chen wrote:

Hi, Youling,

On Mon, Sep 5, 2022 at 8:54 AM Youling Tang  wrote:


Hi, Huacai

On 09/04/2022 08:21 PM, Huacai Chen wrote:

Hi, Youling,

I think crash.c can be merged into crash_dump.c


Most architectures only implement copy_oldmem_page() in crash_dump.c,
I'm not sure if merging crash.c into crash_dump.c will break its
consistency?

Thanks,
Youling

Yes, you are right, crash.c cannot be merged into crash_dump.c, but it
can be merged into machine_kexec.c, as arm64 and riscv do.


For arm64, machine_crash_shutdown() is placed in machine_kexec.c, and
crash_smp_send_stop is placed in smp.c. If crash.c needs to be merged
into machine_kexec.c, should crash_shutdown_secondary and
crash_smp_send_stop be placed in smp.c?

Youling.


Huacai




Huacai

On Mon, Aug 29, 2022 at 12:37 PM Youling Tang  wrote:


This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2320M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  22 ++
 arch/loongarch/Makefile |   4 +
 arch/loongarch/kernel/Makefile  |   3 +-
 arch/loongarch/kernel/crash.c   | 100 
 arch/loongarch/kernel/crash_dump.c  |  19 +
 arch/loongarch/kernel/machine_kexec.c   |  12 ++-
 arch/loongarch/kernel/mem.c |   6 ++
 arch/loongarch/kernel/relocate_kernel.S |   6 ++
 arch/loongarch/kernel/setup.c   |  49 
 arch/loongarch/kernel/traps.c   |   4 +
 10 files changed, 217 insertions(+), 8 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash.c
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 903c82fa958d..7c1b07a5b5bd 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -420,6 +420,28 @@ config KEXEC

  The name comes from the similarity to the exec system call.

+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x90009100" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 4bc47f47cfd8..7dabd580426d 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -48,7 +48,11 @@ KBUILD_CFLAGS_MODULE += -fplt 
-Wa,-mla-global-with-abs,-mla-local-with-abs
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)

+ifdef CONFIG_PHYSICAL_START
+load-y = $(CONFIG_PHYSICAL_START)
+else
 load-y = 0x9020
+endif
 bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y)

 drivers-$(CONFIG_PCI)  += arch/loongarch/pci/
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 20b64ac3f12

Re: [PATCH 1/3] LoongArch: Add kexec support

2022-09-04 Thread Youling Tang




On 08/29/2022 12:37 PM, Youling Tang wrote:

Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to the
LoongArch architecture that add support for the kexec re-boot mechanis
(CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in
PE format.

I tested this on  LoongArch 3A5000 machine and works as expected,

 $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
 $ sudo kexec -e

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  11 ++
 arch/loongarch/include/asm/kexec.h  |  58 
 arch/loongarch/kernel/Makefile  |   2 +
 arch/loongarch/kernel/head.S|   7 +-
 arch/loongarch/kernel/machine_kexec.c   | 178 
 arch/loongarch/kernel/relocate_kernel.S | 125 +
 6 files changed, 380 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 45364cffc793..903c82fa958d 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -409,6 +409,17 @@ config FORCE_MAX_ZONEORDER
  The page size is not necessarily 4KB.  Keep this in mind
  when choosing a value for this option.

+config KEXEC
+   bool "Kexec system call"
+   select KEXEC_CORE
+   help
+ kexec is a system call that implements the ability to shutdown your
+ current kernel, and to start another kernel.  It is like a reboot
+ but it is independent of the system firmware.   And like a reboot
+ you can start any kernel with it, not just Linux.
+
+ The name comes from the similarity to the exec system call.
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h 
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..5c9e7b5eccb8
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+   struct pt_regs *oldregs)
+{
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(*newregs));
+   else
+   prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+   unsigned long boot_flag;
+   unsigned long fdt_addr;
+};
+
+typedef void (*do_kexec_t)(unsigned long boot_flag,
+  unsigned long fdt_addr,
+  unsigned long first_ind_entry,
+  unsigned long jump_addr);
+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+extern void kexec_reboot(void);
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index a213e994db68..20b64ac3f128 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o

+obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+
 obj-$(CONFIG_PROC_FS)  += proc.o

 obj-$(CONFIG_SMP)  += smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index 01bac62a6442..22bdf4928325 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,12 @@

 _head:
.word   MZ_MAGIC/* "MZ", MS-DOS header */
-   .org0x3c/* 0x04 ~ 0x3b reserved */
+   .org0x8
+   .quad   0   /* Image load offset from start of RAM 
*/
+   .dword  _end - _text/* Effective size of kernel image */
+   .quad   0
+   .dword  kernel_entry/* Kernel entry point */
+   .org0x3c/* 0x28 ~ 0x3b reserved */
.long   pe_header - _

Re: [PATCH 2/3] LoongArch: Add kdump support

2022-09-04 Thread Youling Tang

Hi, Huacai

On 09/04/2022 08:21 PM, Huacai Chen wrote:

Hi, Youling,

I think crash.c can be merged into crash_dump.c


Most architectures only implement copy_oldmem_page() in crash_dump.c, 
I'm not sure if merging crash.c into crash_dump.c will break its 
consistency?


Thanks,
Youling



Huacai

On Mon, Aug 29, 2022 at 12:37 PM Youling Tang  wrote:


This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2320M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  22 ++
 arch/loongarch/Makefile |   4 +
 arch/loongarch/kernel/Makefile  |   3 +-
 arch/loongarch/kernel/crash.c   | 100 
 arch/loongarch/kernel/crash_dump.c  |  19 +
 arch/loongarch/kernel/machine_kexec.c   |  12 ++-
 arch/loongarch/kernel/mem.c |   6 ++
 arch/loongarch/kernel/relocate_kernel.S |   6 ++
 arch/loongarch/kernel/setup.c   |  49 
 arch/loongarch/kernel/traps.c   |   4 +
 10 files changed, 217 insertions(+), 8 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash.c
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 903c82fa958d..7c1b07a5b5bd 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -420,6 +420,28 @@ config KEXEC

  The name comes from the similarity to the exec system call.

+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x90009100" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 4bc47f47cfd8..7dabd580426d 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -48,7 +48,11 @@ KBUILD_CFLAGS_MODULE += -fplt 
-Wa,-mla-global-with-abs,-mla-local-with-abs
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)

+ifdef CONFIG_PHYSICAL_START
+load-y = $(CONFIG_PHYSICAL_START)
+else
 load-y = 0x9020
+endif
 bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y)

 drivers-$(CONFIG_PCI)  += arch/loongarch/pci/
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 20b64ac3f128..df5aea129364 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,7 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o

-obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o crash.o
+obj-$(CONFIG_CRASH_DUMP)+= crash_dump.o

 obj-$(CONFIG_PROC_FS)  += proc.o

diff --git a/arch/loonga

Re: [PATCH 1/3] LoongArch: Add kexec support

2022-08-30 Thread Youling Tang



On 08/30/2022 11:25 AM, Youling Tang wrote:

Hi, Jinyang

On 08/30/2022 09:53 AM, Jinyang He wrote:

Hi, Youling,


On 08/29/2022 12:37 PM, Youling Tang wrote:

Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to
the
LoongArch architecture that add support for the kexec re-boot mechanis
(CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in
PE format.

I tested this on  LoongArch 3A5000 machine and works as expected,

  $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
  $ sudo kexec -e

Signed-off-by: Youling Tang 
---
  arch/loongarch/Kconfig  |  11 ++
  arch/loongarch/include/asm/kexec.h  |  58 
  arch/loongarch/kernel/Makefile  |   2 +
  arch/loongarch/kernel/head.S|   7 +-
  arch/loongarch/kernel/machine_kexec.c   | 178 
  arch/loongarch/kernel/relocate_kernel.S | 125 +
  6 files changed, 380 insertions(+), 1 deletion(-)
  create mode 100644 arch/loongarch/include/asm/kexec.h
  create mode 100644 arch/loongarch/kernel/machine_kexec.c
  create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 45364cffc793..903c82fa958d 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -409,6 +409,17 @@ config FORCE_MAX_ZONEORDER
The page size is not necessarily 4KB.  Keep this in mind
when choosing a value for this option.
  +config KEXEC
+bool "Kexec system call"
+select KEXEC_CORE
+help
+  kexec is a system call that implements the ability to shutdown
your
+  current kernel, and to start another kernel.  It is like a reboot
+  but it is independent of the system firmware.   And like a reboot
+  you can start any kernel with it, not just Linux.
+
+  The name comes from the similarity to the exec system call.
+
  config SECCOMP
  bool "Enable seccomp to safely compute untrusted bytecode"
  depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..5c9e7b5eccb8
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+struct pt_regs *oldregs)
+{
+if (oldregs)
+memcpy(newregs, oldregs, sizeof(*newregs));
+else
+prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+unsigned long boot_flag;
+unsigned long fdt_addr;
+};
+
+typedef void (*do_kexec_t)(unsigned long boot_flag,
+   unsigned long fdt_addr,
+   unsigned long first_ind_entry,
+   unsigned long jump_addr);
+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+extern void kexec_reboot(void);
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile
b/arch/loongarch/kernel/Makefile
index a213e994db68..20b64ac3f128 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU)+= fpu.o
  obj-$(CONFIG_MODULES)+= module.o module-sections.o
  obj-$(CONFIG_STACKTRACE)+= stacktrace.o
  +obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+
  obj-$(CONFIG_PROC_FS)+= proc.o
obj-$(CONFIG_SMP)+= smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index 01bac62a6442..22bdf4928325 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,12 @@
_head:
  .wordMZ_MAGIC/* "MZ", MS-DOS header */
-.org0x3c/* 0x04 ~ 0x3b reserved */
+.org0x8
+.quad0/* Image load offset from start of RAM */
+.dword_end - _text/* Effective size of kernel image */
+.quad0
+.dwordkernel_entry/* Kernel entry point */
+.org0x3c/* 0x28 ~ 0x3b reserved */
  .longpe_header - _head/* Offset to the PE header */
  

Re: [PATCH 1/3] LoongArch: Add kexec support

2022-08-29 Thread Youling Tang

Hi, Jinyang

On 08/30/2022 09:53 AM, Jinyang He wrote:

Hi, Youling,


On 08/29/2022 12:37 PM, Youling Tang wrote:

Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to
the
LoongArch architecture that add support for the kexec re-boot mechanis
(CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in
PE format.

I tested this on  LoongArch 3A5000 machine and works as expected,

  $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
  $ sudo kexec -e

Signed-off-by: Youling Tang 
---
  arch/loongarch/Kconfig  |  11 ++
  arch/loongarch/include/asm/kexec.h  |  58 
  arch/loongarch/kernel/Makefile  |   2 +
  arch/loongarch/kernel/head.S|   7 +-
  arch/loongarch/kernel/machine_kexec.c   | 178 
  arch/loongarch/kernel/relocate_kernel.S | 125 +
  6 files changed, 380 insertions(+), 1 deletion(-)
  create mode 100644 arch/loongarch/include/asm/kexec.h
  create mode 100644 arch/loongarch/kernel/machine_kexec.c
  create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 45364cffc793..903c82fa958d 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -409,6 +409,17 @@ config FORCE_MAX_ZONEORDER
The page size is not necessarily 4KB.  Keep this in mind
when choosing a value for this option.
  +config KEXEC
+bool "Kexec system call"
+select KEXEC_CORE
+help
+  kexec is a system call that implements the ability to shutdown
your
+  current kernel, and to start another kernel.  It is like a reboot
+  but it is independent of the system firmware.   And like a reboot
+  you can start any kernel with it, not just Linux.
+
+  The name comes from the similarity to the exec system call.
+
  config SECCOMP
  bool "Enable seccomp to safely compute untrusted bytecode"
  depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..5c9e7b5eccb8
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+struct pt_regs *oldregs)
+{
+if (oldregs)
+memcpy(newregs, oldregs, sizeof(*newregs));
+else
+prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+unsigned long boot_flag;
+unsigned long fdt_addr;
+};
+
+typedef void (*do_kexec_t)(unsigned long boot_flag,
+   unsigned long fdt_addr,
+   unsigned long first_ind_entry,
+   unsigned long jump_addr);
+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+extern void kexec_reboot(void);
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile
b/arch/loongarch/kernel/Makefile
index a213e994db68..20b64ac3f128 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU)+= fpu.o
  obj-$(CONFIG_MODULES)+= module.o module-sections.o
  obj-$(CONFIG_STACKTRACE)+= stacktrace.o
  +obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+
  obj-$(CONFIG_PROC_FS)+= proc.o
obj-$(CONFIG_SMP)+= smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index 01bac62a6442..22bdf4928325 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,12 @@
_head:
  .wordMZ_MAGIC/* "MZ", MS-DOS header */
-.org0x3c/* 0x04 ~ 0x3b reserved */
+.org0x8
+.quad0/* Image load offset from start of RAM */
+.dword_end - _text/* Effective size of kernel image */
+.quad0
+.dwordkernel_entry/* Kernel entry point */
+.org0x3c/* 0x28 ~ 0x3b reserved */
  .longpe_header - _head/* Offset to the PE header */
pe_header:
diff --git a/arch/loongarch/kernel/ma

[PATCH 2/3] LoongArch: Add kdump support

2022-08-28 Thread Youling Tang
This patch adds support for kdump, the kernel will reserve a region
for the crash kernel and jump there on panic.

Arch-specific functions are added to allow for implementing a crash
dump file interface, /proc/vmcore, which can be viewed as a ELF file.

A user space tool, like kexec-tools, is responsible for allocating a
separate region for the core's ELF header within crash kdump kernel
memory and filling it in when executing kexec_load().

Then, its location will be advertised to crash dump kernel via a new
device-tree property, "linux,elfcorehdr", and crash dump kernel preserves
the region for later use with fdt_reserve_elfcorehdr() at boot time.

At the same time, it will also limit the crash kdump kernel to the
crashkernel area via a new device-tree property, "linux, usable-memory-range",
so as not to destroy the original kernel dump data.

On crash dump kernel, /proc/vmcore will access the primary kernel's memory
with copy_oldmem_page().

I tested this on  LoongArch 3A5000 machine and works as expected (Suggest
crashkernel parameter is "crashkernel=512M@2320M"), you may test it by
triggering a crash through /proc/sysrq_trigger:

 $ sudo kexec -p /boot/vmlinux-kdump --reuse-cmdline --append="nr_cpus=1"
 # echo c > /proc/sysrq_trigger

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  22 ++
 arch/loongarch/Makefile |   4 +
 arch/loongarch/kernel/Makefile  |   3 +-
 arch/loongarch/kernel/crash.c   | 100 
 arch/loongarch/kernel/crash_dump.c  |  19 +
 arch/loongarch/kernel/machine_kexec.c   |  12 ++-
 arch/loongarch/kernel/mem.c |   6 ++
 arch/loongarch/kernel/relocate_kernel.S |   6 ++
 arch/loongarch/kernel/setup.c   |  49 
 arch/loongarch/kernel/traps.c   |   4 +
 10 files changed, 217 insertions(+), 8 deletions(-)
 create mode 100644 arch/loongarch/kernel/crash.c
 create mode 100644 arch/loongarch/kernel/crash_dump.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 903c82fa958d..7c1b07a5b5bd 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -420,6 +420,28 @@ config KEXEC
 
  The name comes from the similarity to the exec system call.
 
+config CRASH_DUMP
+   bool "Build kdump crash kernel"
+   help
+ Generate crash dump after being started by kexec. This should
+ be normally only set in special crash dump kernels which are
+ loaded in the main kernel with kexec-tools into a specially
+ reserved region and then later executed after a crash by
+ kdump/kexec.
+
+ For more details see Documentation/admin-guide/kdump/kdump.rst
+
+config PHYSICAL_START
+   hex "Physical address where the kernel is loaded"
+   default "0x90009100" if 64BIT
+   depends on CRASH_DUMP
+   help
+ This gives the XKPRANGE address where the kernel is loaded.
+ If you plan to use kernel for capturing the crash dump change
+ this value to start of the reserved region (the "X" value as
+ specified in the "crashkernel=YM@XM" command line boot parameter
+ passed to the panic-ed kernel).
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile
index 4bc47f47cfd8..7dabd580426d 100644
--- a/arch/loongarch/Makefile
+++ b/arch/loongarch/Makefile
@@ -48,7 +48,11 @@ KBUILD_CFLAGS_MODULE += -fplt 
-Wa,-mla-global-with-abs,-mla-local-with-abs
 cflags-y += -ffreestanding
 cflags-y += $(call cc-option, -mno-check-zero-division)
 
+ifdef CONFIG_PHYSICAL_START
+load-y = $(CONFIG_PHYSICAL_START)
+else
 load-y = 0x9020
+endif
 bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y)
 
 drivers-$(CONFIG_PCI)  += arch/loongarch/pci/
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index 20b64ac3f128..df5aea129364 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,7 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
-obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o crash.o
+obj-$(CONFIG_CRASH_DUMP)+= crash_dump.o
 
 obj-$(CONFIG_PROC_FS)  += proc.o
 
diff --git a/arch/loongarch/kernel/crash.c b/arch/loongarch/kernel/crash.c
new file mode 100644
index ..b4f249ec6301
--- /dev/null
+++ b/arch/loongarch/kernel/crash.c
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ *
+ * Derived from MIPS
+ */
+#include 
+#include 
+#include 
+#includ

[PATCH 0/3] LoongArch: Add kexec/kdump support

2022-08-28 Thread Youling Tang
This patch series to support kexec/kdump (only 64bit).

Kexec is a system call that enables you to load and boot into another kernel
from the currently running kernel. This is useful for kernel developers or
other people who need to reboot very quickly without waiting for the whole
BIOS boot process to finish. 

Kdump uses kexec to quickly boot to a dump-capture kernel whenever a
dump of the system kernel's memory needs to be taken (for example, when
the system panics). The system kernel's memory image is preserved across
the reboot and is accessible to the dump-capture kernel.

For details, see Documentation/admin-guide/kdump/kdump.rst.

User tools kexec-tools see link [1].

TODO:
Currently kdump does not support the same binary image, the production kernel
and the capture kernel will be generated with different configurations. I will
support kernel relocation support in the near future. Then will go to implement
the same binary support based on kernel relocation support.

[1] Link: https://github.com/tangyouling/kexec-tools


Youling Tang (3):
  LoongArch: Add kexec support
  LoongArch: Add kdump support
  LoongArch: Enable CONFIG_KEXEC

 arch/loongarch/Kconfig |  33 
 arch/loongarch/Makefile|   4 +
 arch/loongarch/configs/loongson3_defconfig |   1 +
 arch/loongarch/include/asm/kexec.h |  58 +++
 arch/loongarch/kernel/Makefile |   3 +
 arch/loongarch/kernel/crash.c  | 100 
 arch/loongarch/kernel/crash_dump.c |  19 +++
 arch/loongarch/kernel/head.S   |   7 +-
 arch/loongarch/kernel/machine_kexec.c  | 176 +
 arch/loongarch/kernel/mem.c|   6 +
 arch/loongarch/kernel/relocate_kernel.S| 131 +++
 arch/loongarch/kernel/setup.c  |  49 ++
 arch/loongarch/kernel/traps.c  |   4 +
 13 files changed, 590 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/crash.c
 create mode 100644 arch/loongarch/kernel/crash_dump.c
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH 1/3] LoongArch: Add kexec support

2022-08-28 Thread Youling Tang
Add three new files, kexec.h, machine_kexec.c and relocate_kernel.S to the
LoongArch architecture that add support for the kexec re-boot mechanis
(CONFIG_KEXEC) on LoongArch platforms.

Supports loading vmlinux (vmlinux.elf) in ELF format and vmlinux.efi in
PE format.

I tested this on  LoongArch 3A5000 machine and works as expected,

 $ sudo kexec -l /boot/vmlinux.efi --reuse-cmdline
 $ sudo kexec -e

Signed-off-by: Youling Tang 
---
 arch/loongarch/Kconfig  |  11 ++
 arch/loongarch/include/asm/kexec.h  |  58 
 arch/loongarch/kernel/Makefile  |   2 +
 arch/loongarch/kernel/head.S|   7 +-
 arch/loongarch/kernel/machine_kexec.c   | 178 
 arch/loongarch/kernel/relocate_kernel.S | 125 +
 6 files changed, 380 insertions(+), 1 deletion(-)
 create mode 100644 arch/loongarch/include/asm/kexec.h
 create mode 100644 arch/loongarch/kernel/machine_kexec.c
 create mode 100644 arch/loongarch/kernel/relocate_kernel.S

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 45364cffc793..903c82fa958d 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -409,6 +409,17 @@ config FORCE_MAX_ZONEORDER
  The page size is not necessarily 4KB.  Keep this in mind
  when choosing a value for this option.
 
+config KEXEC
+   bool "Kexec system call"
+   select KEXEC_CORE
+   help
+ kexec is a system call that implements the ability to shutdown your
+ current kernel, and to start another kernel.  It is like a reboot
+ but it is independent of the system firmware.   And like a reboot
+ you can start any kernel with it, not just Linux.
+
+ The name comes from the similarity to the exec system call.
+
 config SECCOMP
bool "Enable seccomp to safely compute untrusted bytecode"
depends on PROC_FS
diff --git a/arch/loongarch/include/asm/kexec.h 
b/arch/loongarch/include/asm/kexec.h
new file mode 100644
index ..5c9e7b5eccb8
--- /dev/null
+++ b/arch/loongarch/include/asm/kexec.h
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * kexec.h for kexec
+ *
+ * Copyright (C) 2022 Loongson Technology Corporation Limited
+ */
+
+#ifndef _ASM_KEXEC_H
+#define _ASM_KEXEC_H
+
+#include 
+#include 
+
+/* Maximum physical address we can use pages from */
+#define KEXEC_SOURCE_MEMORY_LIMIT (-1UL)
+/* Maximum address we can reach in physical address mode */
+#define KEXEC_DESTINATION_MEMORY_LIMIT (-1UL)
+ /* Maximum address we can use for the control code buffer */
+#define KEXEC_CONTROL_MEMORY_LIMIT (-1UL)
+
+/* Reserve a page for the control code buffer */
+#define KEXEC_CONTROL_PAGE_SIZE PAGE_SIZE
+
+/* The native architecture */
+#define KEXEC_ARCH KEXEC_ARCH_LOONGARCH
+
+static inline void crash_setup_regs(struct pt_regs *newregs,
+   struct pt_regs *oldregs)
+{
+   if (oldregs)
+   memcpy(newregs, oldregs, sizeof(*newregs));
+   else
+   prepare_frametrace(newregs);
+}
+
+#define ARCH_HAS_KIMAGE_ARCH
+
+struct kimage_arch {
+   unsigned long boot_flag;
+   unsigned long fdt_addr;
+};
+
+typedef void (*do_kexec_t)(unsigned long boot_flag,
+  unsigned long fdt_addr,
+  unsigned long first_ind_entry,
+  unsigned long jump_addr);
+
+struct kimage;
+extern const unsigned char relocate_new_kernel[];
+extern const size_t relocate_new_kernel_size;
+
+#ifdef CONFIG_SMP
+extern atomic_t kexec_ready_to_reboot;
+extern const unsigned char kexec_smp_wait[];
+extern void kexec_reboot(void);
+#endif
+
+#endif /* !_ASM_KEXEC_H */
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index a213e994db68..20b64ac3f128 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -17,6 +17,8 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o
 obj-$(CONFIG_MODULES)  += module.o module-sections.o
 obj-$(CONFIG_STACKTRACE)   += stacktrace.o
 
+obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o
+
 obj-$(CONFIG_PROC_FS)  += proc.o
 
 obj-$(CONFIG_SMP)  += smp.o
diff --git a/arch/loongarch/kernel/head.S b/arch/loongarch/kernel/head.S
index 01bac62a6442..22bdf4928325 100644
--- a/arch/loongarch/kernel/head.S
+++ b/arch/loongarch/kernel/head.S
@@ -20,7 +20,12 @@
 
 _head:
.word   MZ_MAGIC/* "MZ", MS-DOS header */
-   .org0x3c/* 0x04 ~ 0x3b reserved */
+   .org0x8
+   .quad   0   /* Image load offset from start of RAM 
*/
+   .dword  _end - _text/* Effective size of kernel image */
+   .quad   0
+   .dword  kernel_entry/* Kernel entry point */
+   .org0x3c/* 0x28 ~ 0x3b reserved */
.long   pe_header - _head   /* Offset to the PE header */
 

[PATCH 3/3] LoongArch: Enable CONFIG_KEXEC

2022-08-28 Thread Youling Tang
Defaults enable CONFIG_KEXEC to convenient kexec operations.

Signed-off-by: Youling Tang 
---
 arch/loongarch/configs/loongson3_defconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/loongarch/configs/loongson3_defconfig 
b/arch/loongarch/configs/loongson3_defconfig
index 68c9609670d4..52db7a3a79f3 100644
--- a/arch/loongarch/configs/loongson3_defconfig
+++ b/arch/loongarch/configs/loongson3_defconfig
@@ -45,6 +45,7 @@ CONFIG_SMP=y
 CONFIG_HOTPLUG_CPU=y
 CONFIG_NR_CPUS=64
 CONFIG_NUMA=y
+CONFIG_KEXEC=y
 CONFIG_PAGE_SIZE_16KB=y
 CONFIG_HZ_250=y
 CONFIG_ACPI=y
-- 
2.36.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2 0/4] mips: Fix related problems in kdump operation

2021-04-21 Thread Youling Tang
1. Fix the "mem=" parameter parsing problem.
2. Solve the problem that panic may be triggered due to insufficient memory
when entering the capture kernel.
3. Fix the problem that the captured production kernel data may be destroyed.

Youling Tang (4):
  MIPS: Fix cmdline "mem=" parameter parsing
  mips: kdump: Capture kernel should be able to see old memories
  mips: kdump: Reserve extra memory for crash dump
  mips: kdump: Reserve old memory to avoid the destruction of production
kernel data

 arch/mips/kernel/setup.c | 82 ++--
 1 file changed, 79 insertions(+), 3 deletions(-)

-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2 1/4] MIPS: Fix cmdline "mem=" parameter parsing

2021-04-21 Thread Youling Tang
This problem may only occur on NUMA platforms. When machine start with the
"mem=" parameter on Loongson64, it cannot boot. When parsing the "mem="
parameter, first remove all RAM, and then add memory through memblock_add(),
which causes the newly added memory to be located on MAX_NUMNODES.

The solution is to add the current "mem=" parameter range to the memory area
of the corresponding node, instead of adding all of it to the MAX_NUMNODES
node area. Get the node number corresponding to the "mem=" parameter range
through pa_to_nid(), and then add it to the corresponding node through
memblock_add_node().

Signed-off-by: Jinyang He 
Signed-off-by: Youling Tang 
---
v2:
 - Include the mmzone.h header file, to solve the "error: implicit
   declaration of function'pa_to_nid'" compilation error.

 arch/mips/kernel/setup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 279be01..9338520 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -37,6 +37,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -359,7 +360,7 @@ static int __init early_parse_mem(char *p)
if (*p == '@')
start = memparse(p + 1, &p);
 
-   memblock_add(start, size);
+   memblock_add_node(start, size, pa_to_nid(start));
 
return 0;
 }
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2 4/4] mips: kdump: Reserve old memory to avoid the destruction of production kernel data

2021-04-21 Thread Youling Tang
From: Huacai Chen 

Memory layout:

+-+ end_pfn(e0+128M)
| |
+-+ e0
| |
| |
| |
+-+ e1(crashk_res.start)
| |
| |
| |
+-+ s1(crashk_res.start)
| |
+-+ s0(start_pfn)

[1] When producing the kernel:
Reserve the crashkernel space through crashkernel="YM@XM", so that
[s1, e1] is reserved for the capture kernel.

If the available memory range is greater than 1G, an additional 128M
range is reserved from top to bottom for the capture kernel (ie
[e0, end_pfn] range). The advantage of this is that it can make more
memory available to the capture kernel and avoid triggering insufficient
memory, resulting in panic.

[2] When capturing the kernel:
Finally, the "mem=" parameter is automatically added through kexec-tools
(the "mem=" parameter actually comes from the "crashkernel=" parameter,
and the scope is the same).

It is necessary to reserve the available memory area of the previous
production kernel to avoid the captured data of the production kernel
from being destroyed. If this area in the memory is not reserved, the
captured data will be destroyed, the generated vmcore file is invalid
and cannot be parsed by the crash-utility.

[3] Only consider the memory situation of kdump operation as follows:
1. Production kernel:
memblock.reserve: [s1, e1] and [e0, end_pfn] (Memory is reserved)
memblock.memory:  [s0, s1] and [e1, e0]  (Memory available)

2. Capture kernel:
memblock.reserve: [s0, s1] and [e1, e0]  (Memory is reserved)
memblock.memory:  [s1, e1] and [e0, end_pfn] (Memory available)

In conclusion,[s0, s1] and [e1, e0] memory areas should be reserved.

Signed-off-by: Huacai Chen 
Signed-off-by: Youling Tang 
---
v2:
 - New patch.

 arch/mips/kernel/setup.c | 38 ++
 1 file changed, 38 insertions(+)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index af2c860..aa89f28 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -55,6 +55,8 @@ EXPORT_SYMBOL(cpu_data);
 struct screen_info screen_info;
 #endif
 
+static phys_addr_t crashmem_start, crashmem_size;
+
 /*
  * Setup information
  *
@@ -367,6 +369,11 @@ static int __init early_parse_mem(char *p)
 
memblock_add_node(start, size, pa_to_nid(start));
 
+   if (strstr(boot_command_line, "elfcorehdr") && start && size) {
+   crashmem_start = start;
+   crashmem_size = size;
+   }
+
return 0;
 }
 early_param("mem", early_parse_mem);
@@ -525,6 +532,36 @@ static void reserve_crashm_region(int node, unsigned long 
s0, unsigned long e0)
 }
 #endif /* !defined(CONFIG_KEXEC)  */
 
+/*
+ * After the kdump operation is performed to enter the capture kernel, the
+ * memory area used by the previous production kernel should be reserved to
+ * avoid destroy to the captured data.
+ */
+static void reserve_oldmem_region(int node, unsigned long s0, unsigned long e0)
+{
+   unsigned long s1, e1;
+
+   if (!is_kdump_kernel())
+   return;
+
+   if ((e0 - s0) > (SZ_1G >> PAGE_SHIFT))
+   e0 = e0 - (SZ_128M >> PAGE_SHIFT);
+
+   /* crashmem_start is crashk_res reserved by primary production kernel */
+   s1 = PFN_UP(crashmem_start);
+   e1 = PFN_DOWN(crashmem_start + crashmem_size);
+
+   if (s1 == 0)
+   return;
+
+   if (node == 0) {
+   memblock_reserve(PFN_PHYS(s0), (s1 - s0) << PAGE_SHIFT);
+   memblock_reserve(PFN_PHYS(e1), (e0 - e1) << PAGE_SHIFT);
+   } else {
+   memblock_reserve(PFN_PHYS(s0), (e0 - s0) << PAGE_SHIFT);
+   }
+}
+
 static void __init check_kernel_sections_mem(void)
 {
phys_addr_t start = __pa_symbol(&_text);
@@ -696,6 +733,7 @@ static void __init arch_mem_init(char **cmdline_p)
for_each_online_node(node) {
get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
reserve_crashm_region(node, start_pfn, end_pfn);
+   reserve_oldmem_region(node, start_pfn, end_pfn);
}
 
device_tree_init();
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2 2/4] mips: kdump: Capture kernel should be able to see old memories

2021-04-21 Thread Youling Tang
From: Huacai Chen 

kexec-tools use mem=X@Y to pass usable memories to capture kernel, but
in commit a94e4f24ec836c8984f83959 ("MIPS: init: Drop boot_mem_map") all
BIOS passed memories are removed by early_parse_mem(). I think this is
reasonable for a normal kernel but not for a capture kernel, because a
capture kernel should be able to see all old memories, even though it is
not supposed to use them.

Fixes: a94e4f24ec836c8984f83959 ("MIPS: init: Drop boot_mem_map")
Signed-off-by: Huacai Chen 
Signed-off-by: Youling Tang 
---
v2:
- Determine whether it is a capture kernel by judging whether there
  is an "elfcorehdr" parameter in the cmdline.

 arch/mips/kernel/setup.c | 9 +++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 9338520..1bc8a9cc 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -352,8 +352,13 @@ static int __init early_parse_mem(char *p)
 */
if (usermem == 0) {
usermem = 1;
-   memblock_remove(memblock_start_of_DRAM(),
-   memblock_end_of_DRAM() - memblock_start_of_DRAM());
+   /*
+* During the kdump operation, the old memory should be
+* visible to the capture kernel.
+*/
+   if (!strstr(boot_command_line, "elfcorehdr"))
+   memblock_remove(memblock_start_of_DRAM(),
+   memblock_end_of_DRAM() - 
memblock_start_of_DRAM());
}
start = 0;
size = memparse(p, &p);
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2 3/4] mips: kdump: Reserve extra memory for crash dump

2021-04-21 Thread Youling Tang
From: Huacai Chen 

Traditionally, MIPS's contiguous low memory can be as less as 256M, so
crashkernel=X@Y may be unable to large enough in some cases. Moreover,
for the "multi numa node + sparse memory model" case, it is attempt to
allocate section_mem_maps on every node. Thus, if the total memory of a
node is more than 1GB, we reserve the top 128MB for the capture kernel.

This 128M will be reserved only if the following conditions are met:
1. The configuration option CONFIG_KEXEC is y.
2. A valid "crashkernel=" parameter has been added to the command line.
3. The total memory of a node is greater than 1G.

Signed-off-by: Huacai Chen 
Signed-off-by: Youling Tang 
---
v2:
- New patch.

 arch/mips/kernel/setup.c | 32 
 1 file changed, 32 insertions(+)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 1bc8a9cc..af2c860 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -492,6 +492,25 @@ static void __init request_crashkernel(struct resource 
*res)
(unsigned long)(resource_size(&crashk_res) >> 20),
(unsigned long)(crashk_res.start  >> 20));
 }
+
+/*
+ * Traditionally, MIPS's contiguous low memory is 256M, so crashkernel=X@Y is
+ * unable to be large enough in some cases. Thus, if the total memory of a node
+ * is more than 1GB, we reserve the top 128MB for the capture kernel.
+ */
+static void reserve_crashm_region(int node, unsigned long s0, unsigned long e0)
+{
+   if (crashk_res.start == crashk_res.end)
+   return;
+
+   if ((e0 - s0) <= (SZ_1G >> PAGE_SHIFT))
+   return;
+
+   s0 = e0 - (SZ_128M >> PAGE_SHIFT);
+
+   memblock_reserve(PFN_PHYS(s0), (e0 - s0) << PAGE_SHIFT);
+}
+
 #else /* !defined(CONFIG_KEXEC)*/
 static void __init mips_parse_crashkernel(void)
 {
@@ -500,6 +519,10 @@ static void __init mips_parse_crashkernel(void)
 static void __init request_crashkernel(struct resource *res)
 {
 }
+
+static void reserve_crashm_region(int node, unsigned long s0, unsigned long e0)
+{
+}
 #endif /* !defined(CONFIG_KEXEC)  */
 
 static void __init check_kernel_sections_mem(void)
@@ -627,6 +650,9 @@ static void __init bootcmdline_init(void)
  */
 static void __init arch_mem_init(char **cmdline_p)
 {
+   unsigned int node;
+   unsigned long start_pfn, end_pfn;
+
/* call board setup routine */
plat_mem_setup();
memblock_set_bottom_up(true);
@@ -666,6 +692,12 @@ static void __init arch_mem_init(char **cmdline_p)
if (crashk_res.start != crashk_res.end)
memblock_reserve(crashk_res.start, resource_size(&crashk_res));
 #endif
+
+   for_each_online_node(node) {
+   get_pfn_range_for_nid(node, &start_pfn, &end_pfn);
+   reserve_crashm_region(node, start_pfn, end_pfn);
+   }
+
device_tree_init();
 
/*
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] kexec-tools: mips: Fix the increased mem parameter size

2021-03-16 Thread Youling Tang
The added "mem=size@start" parameter actually corresponds to
"crashkernel=YM@XM", but 1 byte is missing when calculating
the size, so 1 byte should be added.

For example, when using crashkernel=108M@64M (110592K@65536K):
Without this patch:
the mem parameter added is: mem=110591K@65536K

With this patch:
the mem parameter added is: mem=110592K@65536K

Fixes: 0eac64052636 ("kexec: mips: Fix mem parameters")

Signed-off-by: Youling Tang 
---
 kexec/arch/mips/crashdump-mips.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kexec/arch/mips/crashdump-mips.c b/kexec/arch/mips/crashdump-mips.c
index 56b8fe8..aa09c83 100644
--- a/kexec/arch/mips/crashdump-mips.c
+++ b/kexec/arch/mips/crashdump-mips.c
@@ -401,7 +401,7 @@ int load_crashdump_segments(struct kexec_info *info, char* 
mod_cmdline,
 * kernel's available memory
 */
cmdline_add_mem(mod_cmdline, crash_reserved_mem.start,
-   crash_reserved_mem.end - crash_reserved_mem.start);
+   crash_reserved_mem.end - crash_reserved_mem.start + 1);
cmdline_add_elfcorehdr(mod_cmdline, elfcorehdr);
 
dbgprintf("CRASH MEMORY RANGES:\n");
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] MIPS: Fix typo in comment

2021-02-24 Thread Youling Tang
Fix typo in comment.

Signed-off-by: Youling Tang 
---
 kexec/arch/mips/kexec-mips.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kexec/arch/mips/kexec-mips.c b/kexec/arch/mips/kexec-mips.c
index a9c6a09..5866e24 100644
--- a/kexec/arch/mips/kexec-mips.c
+++ b/kexec/arch/mips/kexec-mips.c
@@ -3,7 +3,7 @@
  * Copyright (C) 2007 Francesco Chiechi, Alessandro Rubini
  * Copyright (C) 2007 Tvblob s.r.l.
  *
- * derived from ../ppc/kexec-mips.c
+ * derived from ../ppc/kexec-ppc.c
  * Copyright (C) 2004, 2005 Albert Herranz
  *
  * This source code is licensed under the GNU General Public License,
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] MIPS: Add '--reuse-cmdline' optional parameter support

2021-02-23 Thread Youling Tang
This patch adds an option "--reuse-cmdline" for people that are lazy
in typing --append="$(cat /proc/cmdline)", which will directly use the
command line of the currently running system.

Signed-off-by: Youling Tang 
---
 kexec/arch/mips/include/arch/options.h | 12 +++-
 kexec/arch/mips/kexec-mips.c   |  4 
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/kexec/arch/mips/include/arch/options.h 
b/kexec/arch/mips/include/arch/options.h
index 416e224..ba2f346 100644
--- a/kexec/arch/mips/include/arch/options.h
+++ b/kexec/arch/mips/include/arch/options.h
@@ -1,10 +1,11 @@
 #ifndef KEXEC_ARCH_MIPS_OPTIONS_H
 #define KEXEC_ARCH_MIPS_OPTIONS_H
 
-#define OPT_ARCH_MAX   (OPT_MAX+0)
-#define OPT_APPEND (OPT_ARCH_MAX+0)
-#define OPT_DTB(OPT_ARCH_MAX+1)
-#define OPT_RAMDISK(OPT_ARCH_MAX+2)
+#define OPT_ARCH_MAX   (OPT_MAX+0)
+#define OPT_APPEND (OPT_ARCH_MAX+0)
+#define OPT_DTB(OPT_ARCH_MAX+1)
+#define OPT_RAMDISK(OPT_ARCH_MAX+2)
+#define OPT_REUSE_CMDLINE  (OPT_ARCH_MAX+3)
 
 /* Options relevant to the architecture (excluding loader-specific ones),
  * in this case none:
@@ -14,7 +15,8 @@
{"command-line", 1, 0, OPT_APPEND}, \
{"append",   1, 0, OPT_APPEND}, \
{"dtb", 1, 0, OPT_DTB }, \
-   {"initrd",  1, 0, OPT_RAMDISK },
+   {"initrd",  1, 0, OPT_RAMDISK }, \
+   { "reuse-cmdline", 0, NULL, OPT_REUSE_CMDLINE },
 
 
 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
diff --git a/kexec/arch/mips/kexec-mips.c b/kexec/arch/mips/kexec-mips.c
index 415c2ed..a9c6a09 100644
--- a/kexec/arch/mips/kexec-mips.c
+++ b/kexec/arch/mips/kexec-mips.c
@@ -89,6 +89,7 @@ void arch_usage(void)
"--append=STRING   Set the kernel command line to STRING.\n"
"--dtb=FILEUse FILE as the device tree blob.\n"
"--initrd=FILE Use FILE as initial ramdisk.\n"
+   "--reuse-cmdline   Use kernel command line from running 
system.\n"
);
 }
 
@@ -115,6 +116,9 @@ int arch_process_options(int argc, char **argv)
case OPT_APPEND:
arch_options.command_line = optarg;
break;
+   case OPT_REUSE_CMDLINE:
+   arch_options.command_line = get_command_line();
+   break;
case OPT_DTB:
arch_options.dtb_file = optarg;
break;
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] mips64: Retrieve MAX_PHYSMEM_BITS from vmcoreinfo

2021-02-21 Thread Youling Tang
Add a common feature for architectures to retrieve AX_PHYSMEM_BITS
from vmcoreinfo, which was added by kernel commit 1d50e5d0c505 ("
crash_core, vmcoreinfo: Append 'MAX_PHYSMEM_BITS' to vmcoreinfo").
This makes makedumpfile adaptable for future MAX_PHYSMEM_BITS changes.

Also ensure backward compatibility for kernel versions in which
MAX_PHYSMEM_BITS is not available in vmcoreinfo.

Signed-off-by: Youling Tang 
---
 arch/mips64.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/mips64.c b/arch/mips64.c
index c968082..d541c3e 100644
--- a/arch/mips64.c
+++ b/arch/mips64.c
@@ -35,9 +35,14 @@ get_phys_base_mips64(void)
 int
 get_machdep_info_mips64(void)
 {
-   info->max_physmem_bits  = _MAX_PHYSMEM_BITS;
info->section_size_bits = _SECTION_SIZE_BITS;
 
+   /* Check if we can get MAX_PHYSMEM_BITS from vmcoreinfo */
+   if (NUMBER(MAX_PHYSMEM_BITS) != NOT_FOUND_NUMBER)
+   info->max_physmem_bits = NUMBER(MAX_PHYSMEM_BITS);
+   else
+   info->max_physmem_bits  = _MAX_PHYSMEM_BITS;
+
DEBUG_MSG("max_physmem_bits : %lx\n", info->max_physmem_bits);
DEBUG_MSG("section_size_bits: %lx\n", info->section_size_bits);
 
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2] makedumpfile: Add initial mips64 support

2021-01-29 Thread Youling Tang
Patch adds support for mips64 in makedumpfile. It takes care of vmalloc,
module and directly map kernel memory region's translation. Currently we
only support 3 leverl 16K pages and VA_BITS as 48.

The changes were tested on a mips64 Loongson-3A4000 processor. The dump
compression and filtering (for all dump levels 1,2,4,8,16 and 31) tests
are succussfull.

Signed-off-by: Huacai Chen 
Signed-off-by: Jinyang He 
Signed-off-by: Youling Tang 
---
v1 -> v2:
 - Move max_physmem_bits and section_size_bits to get_machdep_info_mips64().

 Makefile   |   2 +-
 arch/mips64.c  | 121 +
 makedumpfile.h |  55 ++
 3 files changed, 177 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips64.c

diff --git a/Makefile b/Makefile
index cb6bd42..6f8ade0 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ endif
 SRC_BASE = makedumpfile.c makedumpfile.h diskdump_mod.h sadump_mod.h 
sadump_info.h
 SRC_PART = print_info.c dwarf_info.c elf_info.c erase_info.c sadump_info.c 
cache.c tools.c printk.c
 OBJ_PART=$(patsubst %.c,%.o,$(SRC_PART))
-SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c arch/ppc.c arch/sparc64.c
+SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c arch/ppc.c arch/sparc64.c arch/mips64.c
 OBJ_ARCH=$(patsubst %.c,%.o,$(SRC_ARCH))
 
 LIBS = -ldw -lbz2 -ldl -lelf -lz
diff --git a/arch/mips64.c b/arch/mips64.c
new file mode 100644
index 000..c968082
--- /dev/null
+++ b/arch/mips64.c
@@ -0,0 +1,121 @@
+/*
+ * mips64.c
+ *
+ * Copyright (C) 2021 Loongson Technology Co., Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation (version 2 of the License).
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#ifdef __mips64__
+
+#include "../print_info.h"
+#include "../elf_info.h"
+#include "../makedumpfile.h"
+
+int
+get_phys_base_mips64(void)
+{
+   info->phys_base = 0ULL;
+
+   DEBUG_MSG("phys_base: %lx\n", info->phys_base);
+
+   return TRUE;
+}
+
+int
+get_machdep_info_mips64(void)
+{
+   info->max_physmem_bits  = _MAX_PHYSMEM_BITS;
+   info->section_size_bits = _SECTION_SIZE_BITS;
+
+   DEBUG_MSG("max_physmem_bits : %lx\n", info->max_physmem_bits);
+   DEBUG_MSG("section_size_bits: %lx\n", info->section_size_bits);
+
+   return TRUE;
+}
+
+int
+get_versiondep_info_mips64(void)
+{
+   info->page_offset  = 0x9800ULL;
+
+   DEBUG_MSG("page_offset : %lx\n", info->page_offset);
+
+   return TRUE;
+}
+
+/*
+ * Translate a virtual address to a physical address by using 3 levels paging.
+ */
+unsigned long long
+vaddr_to_paddr_mips64(unsigned long vaddr)
+{
+   unsigned long long paddr = NOT_PADDR;
+   pgd_t   *pgda, pgdv;
+   pmd_t   *pmda, pmdv;
+   pte_t   *ptea, ptev;
+
+   /*
+* CKSEG0/CKSEG1
+*/
+   if (vaddr >= 0x8000ULL && vaddr < 0xc000ULL)
+   return vaddr & 0x1fffULL;
+
+   /*
+* XKPHYS
+*/
+   if (vaddr >= 0x9000ULL && vaddr < 0xc000ULL)
+   return vaddr & ((1ULL << MAX_PHYSMEM_BITS()) - 1);
+
+   if (SYMBOL(swapper_pg_dir) == NOT_FOUND_SYMBOL) {
+   ERRMSG("Can't get the symbol of swapper_pg_dir.\n");
+   return NOT_PADDR;
+   }
+
+   pgda = pgd_offset(SYMBOL(swapper_pg_dir), vaddr);
+   if (!readmem(PADDR, (unsigned long long)pgda, &pgdv, sizeof(pgdv))) {
+   ERRMSG("Can't read pgd\n");
+   return NOT_PADDR;
+   }
+
+   pmda = pmd_offset(&pgdv, vaddr);
+   if (!readmem(PADDR, (unsigned long long)pmda, &pmdv, sizeof(pmdv))) {
+   ERRMSG("Can't read pmd\n");
+   return NOT_PADDR;
+   }
+
+   switch (pmdv & (_PAGE_PRESENT|_PAGE_HUGE)) {
+   case _PAGE_PRESENT:
+   ptea = pte_offset(&pmdv, vaddr);
+   /* 64k page */
+   if (!readmem(PADDR, (unsigned long long)ptea, &ptev, 
sizeof(ptev))) {
+   ERRMSG("Can't read pte\n");
+   return NOT_PADDR;
+   }
+
+   if 

Re: [PATCH] makedumpfile: Add initial mips64 support

2021-01-28 Thread Youling Tang

Hi HAGIO KAZUHITO,

On 01/29/2021 02:13 PM, HAGIO KAZUHITO(萩尾 一仁) wrote:

Hi Yuling Tang,

-Original Message-

Patch adds support for mips64 in makedumpfile. It takes care of vmalloc,
module and directly map kernel memory region's translation. Currently we
only support 3 leverl 16K pages and VA_BITS as 48.

The changes were tested on a mips64 Loongson-3A4000 processor. The dump
compression and filtering (for all dump levels 1,2,4,8,16 and 31) tests
are succussfull.

Signed-off-by: Huacai Chen 
Signed-off-by: Jinyang He 
Signed-off-by: Youling Tang 
---
  Makefile   |   2 +-
  arch/mips64.c  | 113 +
  makedumpfile.h |  54 +++
  3 files changed, 168 insertions(+), 1 deletion(-)
  create mode 100644 arch/mips64.c

diff --git a/Makefile b/Makefile
index cb6bd42..6f8ade0 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ endif
  SRC_BASE = makedumpfile.c makedumpfile.h diskdump_mod.h sadump_mod.h 
sadump_info.h
  SRC_PART = print_info.c dwarf_info.c elf_info.c erase_info.c sadump_info.c 
cache.c tools.c printk.c
  OBJ_PART=$(patsubst %.c,%.o,$(SRC_PART))
-SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c
arch/ppc.c arch/sparc64.c
+SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c
arch/ppc.c arch/sparc64.c arch/mips64.c
  OBJ_ARCH=$(patsubst %.c,%.o,$(SRC_ARCH))

  LIBS = -ldw -lbz2 -ldl -lelf -lz
diff --git a/arch/mips64.c b/arch/mips64.c
new file mode 100644
index 000..18a9e05
--- /dev/null
+++ b/arch/mips64.c
@@ -0,0 +1,113 @@
+/*
+ * mips64.c
+ *
+ * Copyright (C) 2021 Loongson Technology Co., Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation (version 2 of the License).
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#ifdef __mips64__
+
+#include "../print_info.h"
+#include "../elf_info.h"
+#include "../makedumpfile.h"
+
+int
+get_phys_base_mips64(void)
+{
+   info->phys_base = 0ULL;
+
+   DEBUG_MSG("phys_base: %lx\n", info->phys_base);
+
+   return TRUE;
+}
+
+int
+get_versiondep_info_mips64(void)
+{
+   info->page_offset  = 0x9800ULL;
+   info->max_physmem_bits  = _MAX_PHYSMEM_BITS;
+   info->section_size_bits = _SECTION_SIZE_BITS;
+
+   DEBUG_MSG("page_offset : %lx\n", info->page_offset);
+   DEBUG_MSG("max_physmem_bits : %lx\n", info->max_physmem_bits);
+   DEBUG_MSG("section_size_bits: %lx\n", info->section_size_bits);
+
+   return TRUE;
+}
+
+/*
+ * Translate a virtual address to a physical address by using 3 levels paging.
+ */
+unsigned long long
+vaddr_to_paddr_mips64(unsigned long vaddr)
+{
+   unsigned long long paddr = NOT_PADDR;
+   pgd_t   *pgda, pgdv;
+   pmd_t   *pmda, pmdv;
+   pte_t   *ptea, ptev;
+
+   /*
+* CKSEG0/CKSEG1
+*/
+   if (vaddr >= 0x8000ULL && vaddr < 0xc000ULL)
+   return vaddr & 0x1fffULL;
+
+   /*
+* XKPHYS
+*/
+   if (vaddr >= 0x9000ULL && vaddr < 0xc000ULL)
+   return vaddr & ((1ULL << MAX_PHYSMEM_BITS()) - 1);

 From the current implementation, vaddr_to_paddr() is called the first time
in the context of:

initial()
   get_machdep_info()
   calibrate_machdep_info()
   check_release()
 readmem()
   vaddr_to_paddr()
   get_versiondep_info()

So setting info->max_physmem_bits in get_versiondep_info() is late,
but it might work because
- The address that the readmem() reads is SYMBOL(init_uts_ns), so
   this XKPHYS condition does not match,  or/and
- 5.9+ kernels have NUMBER(MAX_PHYSMEM_BITS) entry in vmcoreinfo, so
   info->max_physmem_bits is set in calibrate_machdep_info().

So I would suggest moving max_physmem_bits and section_size_bits to
get_machdep_info() for future changes and to not confuse readers.. (including 
me :-)

Otherwise, looks good to me.  Thank you for using makedumpfile!

Thank you for your reply and suggestions.
I will move max_physmem_bits and section_size_bits to
get_machdep_info_mips64() in V2.

Thanks,
Youling.


Kazu


+
+   if (SYMBOL(swapper_pg_dir) == NOT_FOUND_SYMBOL) {
+   ERRMSG("Can't get the symbol of swapper_pg_dir.\n");
+

[PATCH] makedumpfile: Add initial mips64 support

2021-01-18 Thread Youling Tang
Patch adds support for mips64 in makedumpfile. It takes care of vmalloc,
module and directly map kernel memory region's translation. Currently we
only support 3 leverl 16K pages and VA_BITS as 48.

The changes were tested on a mips64 Loongson-3A4000 processor. The dump
compression and filtering (for all dump levels 1,2,4,8,16 and 31) tests
are succussfull.

Signed-off-by: Huacai Chen 
Signed-off-by: Jinyang He 
Signed-off-by: Youling Tang 
---
 Makefile   |   2 +-
 arch/mips64.c  | 113 +
 makedumpfile.h |  54 +++
 3 files changed, 168 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips64.c

diff --git a/Makefile b/Makefile
index cb6bd42..6f8ade0 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ endif
 SRC_BASE = makedumpfile.c makedumpfile.h diskdump_mod.h sadump_mod.h 
sadump_info.h
 SRC_PART = print_info.c dwarf_info.c elf_info.c erase_info.c sadump_info.c 
cache.c tools.c printk.c
 OBJ_PART=$(patsubst %.c,%.o,$(SRC_PART))
-SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c arch/ppc.c arch/sparc64.c
+SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c arch/ppc.c arch/sparc64.c arch/mips64.c
 OBJ_ARCH=$(patsubst %.c,%.o,$(SRC_ARCH))
 
 LIBS = -ldw -lbz2 -ldl -lelf -lz
diff --git a/arch/mips64.c b/arch/mips64.c
new file mode 100644
index 000..18a9e05
--- /dev/null
+++ b/arch/mips64.c
@@ -0,0 +1,113 @@
+/*
+ * mips64.c
+ *
+ * Copyright (C) 2021 Loongson Technology Co., Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation (version 2 of the License).
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+#ifdef __mips64__
+
+#include "../print_info.h"
+#include "../elf_info.h"
+#include "../makedumpfile.h"
+
+int
+get_phys_base_mips64(void)
+{
+   info->phys_base = 0ULL;
+
+   DEBUG_MSG("phys_base: %lx\n", info->phys_base);
+
+   return TRUE;
+}
+
+int
+get_versiondep_info_mips64(void)
+{
+   info->page_offset  = 0x9800ULL;
+   info->max_physmem_bits  = _MAX_PHYSMEM_BITS;
+   info->section_size_bits = _SECTION_SIZE_BITS;
+
+   DEBUG_MSG("page_offset : %lx\n", info->page_offset);
+   DEBUG_MSG("max_physmem_bits : %lx\n", info->max_physmem_bits);
+   DEBUG_MSG("section_size_bits: %lx\n", info->section_size_bits);
+
+   return TRUE;
+}
+
+/*
+ * Translate a virtual address to a physical address by using 3 levels paging.
+ */
+unsigned long long
+vaddr_to_paddr_mips64(unsigned long vaddr)
+{
+   unsigned long long paddr = NOT_PADDR;
+   pgd_t   *pgda, pgdv;
+   pmd_t   *pmda, pmdv;
+   pte_t   *ptea, ptev;
+
+   /*
+* CKSEG0/CKSEG1
+*/
+   if (vaddr >= 0x8000ULL && vaddr < 0xc000ULL)
+   return vaddr & 0x1fffULL;
+
+   /*
+* XKPHYS
+*/
+   if (vaddr >= 0x9000ULL && vaddr < 0xc000ULL)
+   return vaddr & ((1ULL << MAX_PHYSMEM_BITS()) - 1);
+
+   if (SYMBOL(swapper_pg_dir) == NOT_FOUND_SYMBOL) {
+   ERRMSG("Can't get the symbol of swapper_pg_dir.\n");
+   return NOT_PADDR;
+   }
+
+   pgda = pgd_offset(SYMBOL(swapper_pg_dir), vaddr);
+   if (!readmem(PADDR, (unsigned long long)pgda, &pgdv, sizeof(pgdv))) {
+   ERRMSG("Can't read pgd\n");
+   return NOT_PADDR;
+   }
+
+   pmda = pmd_offset(&pgdv, vaddr);
+   if (!readmem(PADDR, (unsigned long long)pmda, &pmdv, sizeof(pmdv))) {
+   ERRMSG("Can't read pmd\n");
+   return NOT_PADDR;
+   }
+
+   switch (pmdv & (_PAGE_PRESENT|_PAGE_HUGE)) {
+   case _PAGE_PRESENT:
+   ptea = pte_offset(&pmdv, vaddr);
+   /* 64k page */
+   if (!readmem(PADDR, (unsigned long long)ptea, &ptev, 
sizeof(ptev))) {
+   ERRMSG("Can't read pte\n");
+   return NOT_PADDR;
+   }
+
+   if (!(ptev & _PAGE_PRESENT)) {
+   ERRMSG("Can't get a valid pte.\n");
+   return NOT_PADDR;
+   

Re: [PATCH] kexec-tools: Add some missing free() calls

2020-09-16 Thread Youling Tang




On 09/17/2020 04:20 AM, Bhupesh SHARMA wrote:

Hi Youling,

See some comments inline:

On Sat, Sep 12, 2020 at 7:11 AM Youling Tang  wrote:

Add some missing free() calls.

Signed-off-by: Youling Tang 
---
  kexec/arch/i386/crashdump-x86.c| 22 +-
  kexec/arch/mips/crashdump-mips.c   |  5 -
  kexec/arch/ppc64/crashdump-ppc64.c |  8 ++--
  3 files changed, 27 insertions(+), 8 deletions(-)

First, I think this is a step in the right direction, however, earlier
also while running 'valgrind' on an x86_64 kexec elf I saw the
following memory leaks reported:

==596886== 15,604 bytes in 1 blocks are indirectly lost in loss record 4 of 12
==596886==at 0x483A809: malloc (vg_replace_malloc.c:307)
==596886==by 0x40396D: xmalloc (kexec.c:101)
==596886==by 0x410D35: do_bzImage64_load (kexec-bzImage64.c:182)
==596886==by 0x410D35: bzImage64_load (kexec-bzImage64.c:391)
==596886==by 0x404410: my_load (kexec.c:774)
==596886==by 0x402D2D: main (kexec.c:1605)

==596886== 15,732 (128 direct, 15,604 indirect) bytes in 1 blocks are
definitely lost in loss record 5 of 12
==596886==at 0x483CCE8: realloc (vg_replace_malloc.c:834)
==596886==by 0x403FB9: xrealloc (kexec.c:112)
==596886==by 0x403FB9: add_segment_phys_virt (kexec.c:357)
==596886==by 0x40410F: add_buffer_phys_virt (kexec.c:392)
==596886==by 0x404153: add_buffer_virt (kexec.c:401)
==596886==by 0x40CD11: setup_linux_bootloader_parameters_high
(x86-linux-setup.c:80)
==596886==by 0x410E6A: do_bzImage64_load (kexec-bzImage64.c:214)
==596886==by 0x410E6A: bzImage64_load (kexec-bzImage64.c:391)
==596886==by 0x404410: my_load (kexec.c:774)
==596886==by 0x402D2D: main (kexec.c:1605)

==596886== 28,896 bytes in 1 blocks are indirectly lost in loss record 7 of 12
==596886==at 0x483A809: malloc (vg_replace_malloc.c:307)
==596886==by 0x40396D: xmalloc (kexec.c:101)
==596886==by 0x406781: elf_rel_load (kexec-elf-rel.c:254)
==596886==by 0x406EEA: elf_rel_build_load (kexec-elf-rel.c:432)
==596886==by 0x410CFE: do_bzImage64_load (kexec-bzImage64.c:173)
==596886==by 0x410CFE: bzImage64_load (kexec-bzImage64.c:391)
==596886==by 0x404410: my_load (kexec.c:774)
==596886==by 0x402D2D: main (kexec.c:1605)

==596886== 30,048 (1,152 direct, 28,896 indirect) bytes in 1 blocks
are definitely lost in loss record 8 of 12
==596886==at 0x483A809: malloc (vg_replace_malloc.c:307)
==596886==by 0x40396D: xmalloc (kexec.c:101)
==596886==by 0x405735: build_mem_shdrs (kexec-elf.c:618)
==596886==by 0x405735: build_elf_info (kexec-elf.c:774)
==596886==by 0x406EB9: build_elf_rel_info (kexec-elf-rel.c:142)
==596886==by 0x406EB9: elf_rel_build_load (kexec-elf-rel.c:427)
==596886==by 0x410CFE: do_bzImage64_load (kexec-bzImage64.c:173)
==596886==by 0x410CFE: bzImage64_load (kexec-bzImage64.c:391)
==596886==by 0x404410: my_load (kexec.c:774)
==596886==by 0x402D2D: main (kexec.c:1605)
==596886==

Note that there were 12 issues highlighted via valgrind out of which I
have removed the zlib related issue reports.

You can run 'valgrind' on mips, i386 and ppc64 executables (as shown
below) to see if all such issues are fixed by your patch:
$ sudo valgrind --leak-check=full --show-leak-kinds=all
--track-origins=yes --verbose --log-file=valgrind-out.txt ./kexec -l
/boot/vmlinuz-`uname -r` --initrd=/boot/initramfs-`uname -r`.img
--reuse-cmdline -d


Hi Bhupesh,

Thank you for your advice.
But it fails when installing 'valgrind' tool (arch: mips64),
the compilation error message is as follows:
...
/tmp/ccF1OhPy.s:6605: Error: unrecognized opcode `cfcmsa $4,$1'
/tmp/ccF1OhPy.s:6606: Error: unrecognized opcode `ctcmsa $1,$t0'
/tmp/ccF1OhPy.s:6656: Warning: tried to set unrecognized symbol: msa
/tmp/ccF1OhPy.s:6658: Error: unrecognized opcode `cfcmsa $2,$0'
Makefile:1625: recipe for target 
'priv/libvex_mips64_linux_a-guest_mips_helpers.o' failed
make[3]: *** [priv/libvex_mips64_linux_a-guest_mips_helpers.o] Error 1
...

Youling,
Thanks.


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] kexec-tools: Add some missing free() calls

2020-09-11 Thread Youling Tang
Add some missing free() calls.

Signed-off-by: Youling Tang 
---
 kexec/arch/i386/crashdump-x86.c| 22 +-
 kexec/arch/mips/crashdump-mips.c   |  5 -
 kexec/arch/ppc64/crashdump-ppc64.c |  8 ++--
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
index c79791f..d5b5b68 100644
--- a/kexec/arch/i386/crashdump-x86.c
+++ b/kexec/arch/i386/crashdump-x86.c
@@ -913,8 +913,11 @@ int load_crashdump_segments(struct kexec_info *info, char* 
mod_cmdline,
add_memmap(memmap_p, &nr_memmap, info->backup_src_start, 
info->backup_src_size, RANGE_RAM);
for (i = 0; i < crash_reserved_mem_nr; i++) {
sz = crash_reserved_mem[i].end - crash_reserved_mem[i].start +1;
-   if (add_memmap(memmap_p, &nr_memmap, 
crash_reserved_mem[i].start, sz, RANGE_RAM) < 0)
+   if (add_memmap(memmap_p, &nr_memmap, 
crash_reserved_mem[i].start,
+   sz, RANGE_RAM) < 0) {
+   free(memmap_p);
return ENOCRASHKERNEL;
+   }
}
 
/* Create a backup region segment to store backup data*/
@@ -926,22 +929,29 @@ int load_crashdump_segments(struct kexec_info *info, 
char* mod_cmdline,
0, max_addr, -1);
dbgprintf("Created backup segment at 0x%lx\n",
  info->backup_start);
-   if (delete_memmap(memmap_p, &nr_memmap, info->backup_start, sz) 
< 0)
+   if (delete_memmap(memmap_p, &nr_memmap, info->backup_start, sz) 
< 0) {
+   free(tmp);
+   free(memmap_p);
return EFAILED;
+   }
}
 
/* Create elf header segment and store crash image data. */
if (arch_options.core_header_type == CORE_TYPE_ELF64) {
if (crash_create_elf64_headers(info, &elf_info, mem_range,
nr_ranges, &tmp, &bufsz,
-   ELF_CORE_HEADER_ALIGN) < 0)
+   ELF_CORE_HEADER_ALIGN) < 0) {
+   free(memmap_p);
return EFAILED;
+   }
}
else {
if (crash_create_elf32_headers(info, &elf_info, mem_range,
nr_ranges, &tmp, &bufsz,
-   ELF_CORE_HEADER_ALIGN) < 0)
+   ELF_CORE_HEADER_ALIGN) < 0) {
+   free(memmap_p);
return EFAILED;
+   }
}
/* the size of the elf headers allocated is returned in 'bufsz' */
 
@@ -962,8 +972,10 @@ int load_crashdump_segments(struct kexec_info *info, char* 
mod_cmdline,
elfcorehdr = add_buffer(info, tmp, bufsz, memsz, align, min_base,
max_addr, -1);
dbgprintf("Created elf header segment at 0x%lx\n", elfcorehdr);
-   if (delete_memmap(memmap_p, &nr_memmap, elfcorehdr, memsz) < 0)
+   if (delete_memmap(memmap_p, &nr_memmap, elfcorehdr, memsz) < 0) {
+   free(memmap_p);
return -1;
+   }
if (!bzImage_support_efi_boot || arch_options.noefi ||
!sysfs_efi_runtime_map_exist())
cmdline_add_efi(mod_cmdline);
diff --git a/kexec/arch/mips/crashdump-mips.c b/kexec/arch/mips/crashdump-mips.c
index fc92e64..c1603d9 100644
--- a/kexec/arch/mips/crashdump-mips.c
+++ b/kexec/arch/mips/crashdump-mips.c
@@ -387,8 +387,11 @@ int load_crashdump_segments(struct kexec_info *info, char* 
mod_cmdline,
crash_reserved_mem.end, -1);
 
if (crash_create(info, elf_info, crash_memory_range, nr_ranges,
-&tmp, &sz, ELF_CORE_HEADER_ALIGN) < 0)
+&tmp, &sz, ELF_CORE_HEADER_ALIGN) < 0) {
+   free(tmp);
return -1;
+   }
+
elfcorehdr = add_buffer(info, tmp, sz, sz, align,
crash_reserved_mem.start,
crash_reserved_mem.end, -1);
diff --git a/kexec/arch/ppc64/crashdump-ppc64.c 
b/kexec/arch/ppc64/crashdump-ppc64.c
index b2787d5..26f9a01 100644
--- a/kexec/arch/ppc64/crashdump-ppc64.c
+++ b/kexec/arch/ppc64/crashdump-ppc64.c
@@ -535,15 +535,19 @@ int load_crashdump_segments(struct kexec_info *info, 
char* mod_cmdline,
if (crash_create_elf64_headers(info, &elf_info64,
   crash_memory_range, nr_ranges,
   &tmp, &sz,
-

[PATCH] kexec-tools: Fix a prompt message when crashkernel is not reserved

2020-09-11 Thread Youling Tang
Where Y specifies how much memory to reserve for the dump-capture kernel
and X specifies the beginning of this reserved memory. So Y should be
placed before X.

Signed-off-by: Youling Tang 
---
 kexec/kexec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index bb88caa..fd7c8d2 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -1530,7 +1530,7 @@ int main(int argc, char *argv[])
!is_crashkernel_mem_reserved()) {
die("Memory for crashkernel is not reserved\n"
"Please reserve memory by passing"
-   "\"crashkernel=X@Y\" parameter to kernel\n"
+   "\"crashkernel=Y@X\" parameter to kernel\n"
"Then try to loading kdump kernel\n");
}
 
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] kexec-tools: mips: Remove commandline parameter "mem"

2020-09-11 Thread Youling Tang
"mem=" indicating the memory region the new kernel can use to boot into.
And passed to the dump-capture kernel by kernel commandline parameter
"mem=". But in the dump-capture kernel, we don’t need to use this parameter
now, so remove "mem" and don't add "mem=" to new kernel commandline.

Signed-off-by: Youling Tang 
---
 kexec/arch/mips/crashdump-mips.c | 29 -
 1 file changed, 29 deletions(-)

diff --git a/kexec/arch/mips/crashdump-mips.c b/kexec/arch/mips/crashdump-mips.c
index fc92e64..26d5043 100644
--- a/kexec/arch/mips/crashdump-mips.c
+++ b/kexec/arch/mips/crashdump-mips.c
@@ -243,33 +243,6 @@ static void ultoa(unsigned long i, char *str)
}
 }
 
-/* Adds the appropriate mem= options to command line, indicating the
- * memory region the new kernel can use to boot into. */
-static int cmdline_add_mem(char *cmdline, unsigned long addr,
-   unsigned long size)
-{
-   int cmdlen, len;
-   char str[50], *ptr;
-
-   addr = addr/1024;
-   size = size/1024;
-   ptr = str;
-   strcpy(str, " mem=");
-   ptr += strlen(str);
-   ultoa(size, ptr);
-   strcat(str, "K@");
-   ptr = str + strlen(str);
-   ultoa(addr, ptr);
-   strcat(str, "K");
-   len = strlen(str);
-   cmdlen = strlen(cmdline) + len;
-   if (cmdlen > (COMMAND_LINE_SIZE - 1))
-   die("Command line overflow\n");
-   strcat(cmdline, str);
-
-   return 0;
-}
-
 /* Adds the elfcorehdr= command line parameter to command line. */
 static int cmdline_add_elfcorehdr(char *cmdline, unsigned long addr)
 {
@@ -397,8 +370,6 @@ int load_crashdump_segments(struct kexec_info *info, char* 
mod_cmdline,
 * backup segment is after elfcorehdr, so use elfcorehdr as top of
 * kernel's available memory
 */
-   cmdline_add_mem(mod_cmdline, crash_reserved_mem.start,
-   elfcorehdr - crash_reserved_mem.start);
cmdline_add_elfcorehdr(mod_cmdline, elfcorehdr);
 
dbgprintf("CRASH MEMORY RANGES:\n");
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2] kexec/kexec.c: Add missing close() call

2020-08-25 Thread Youling Tang
Add missing close() call.

Signed-off-by: Youling Tang 
---
 kexec/kexec.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index a62b362..bb88caa 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -585,6 +585,7 @@ static char *slurp_file_generic(const char *filename, off_t 
*r_size,
die("Read on %s ended before stat said it should\n", filename);
 
*r_size = size;
+   close(fd);
return buf;
 }
 
@@ -1257,12 +1258,14 @@ static int do_kexec_file_load(int fileind, int argc, 
char **argv,
if (i == file_types) {
fprintf(stderr, "Cannot determine the file type " "of %s\n",
kernel);
+   close(kernel_fd);
return EFAILED;
}
 
ret = file_type[i].load(argc, argv, kernel_buf, kernel_size, &info);
if (ret < 0) {
fprintf(stderr, "Cannot load %s\n", kernel);
+   close(kernel_fd);
return ret;
}
 
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] kexec-tools: Fix resource leaks in kexec.c

2020-08-25 Thread Youling Tang
Add free() call when handling some error returns.

Signed-off-by: Youling Tang 
---
 kexec/kexec.c | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index a62b362..a0f0908 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -1128,11 +1128,15 @@ char *get_command_line(void)
die("Could not allocate memory to read /proc/cmdline.");
 
fp = fopen("/proc/cmdline", "r");
-   if (!fp)
+   if (!fp) {
+   free(line);
die("Could not open /proc/cmdline.");
+   }
 
-   if (fgets(line, sizeof_line, fp) == NULL)
+   if (fgets(line, sizeof_line, fp) == NULL) {
+   free(line);
die("Can't read /proc/cmdline.");
+   }
 
fclose(fp);
 
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] kexec/kexec.c: Add missing fclose()/close() call

2020-08-25 Thread Youling Tang
Add missing fclose()/close() call.

Signed-off-by: Youling Tang 
---
 kexec/kexec.c | 28 ++--
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index a62b362..f970b13 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -542,6 +542,7 @@ static char *slurp_file_generic(const char *filename, off_t 
*r_size,
}
result = fstat(fd, &stats);
if (result < 0) {
+   close(fd);
die("Cannot stat: %s: %s\n",
filename, strerror(errno));
}
@@ -553,20 +554,26 @@ static char *slurp_file_generic(const char *filename, 
off_t *r_size,
if (S_ISCHR(stats.st_mode)) {
 
size = lseek(fd, 0, SEEK_END);
-   if (size < 0)
+   if (size < 0) {
+   close(fd);
die("Can not seek file %s: %s\n", filename,
strerror(errno));
+   }
 
err = lseek(fd, 0, SEEK_SET);
-   if (err < 0)
+   if (err < 0) {
+   close(fd);
die("Can not seek to the begin of file %s: %s\n",
filename, strerror(errno));
+   }
buf = slurp_fd(fd, filename, size, &nread);
} else if (S_ISBLK(stats.st_mode)) {
err = ioctl(fd, BLKGETSIZE64, &size);
-   if (err < 0)
+   if (err < 0) {
+   close(fd);
die("Can't retrieve size of block device %s: %s\n",
filename, strerror(errno));
+   }
buf = slurp_fd(fd, filename, size, &nread);
} else {
size = stats.st_size;
@@ -578,13 +585,18 @@ static char *slurp_file_generic(const char *filename, 
off_t *r_size,
buf = slurp_fd(fd, filename, size, &nread);
}
}
-   if ((use_mmap && (buf == MAP_FAILED)) || (!use_mmap && (buf == NULL)))
+   if ((use_mmap && (buf == MAP_FAILED)) || (!use_mmap && (buf == NULL))) {
+   close(fd);
die("Cannot read %s", filename);
+   }
 
-   if (nread != size)
+   if (nread != size) {
+   close(fd);
die("Read on %s ended before stat said it should\n", filename);
+   }
 
*r_size = size;
+   close(fd);
return buf;
 }
 
@@ -1131,8 +1143,10 @@ char *get_command_line(void)
if (!fp)
die("Could not open /proc/cmdline.");
 
-   if (fgets(line, sizeof_line, fp) == NULL)
+   if (fgets(line, sizeof_line, fp) == NULL) {
+   fclose(fp);
die("Can't read /proc/cmdline.");
+   }
 
fclose(fp);
 
@@ -1257,12 +1271,14 @@ static int do_kexec_file_load(int fileind, int argc, 
char **argv,
if (i == file_types) {
fprintf(stderr, "Cannot determine the file type " "of %s\n",
kernel);
+   close(kernel_fd);
return EFAILED;
}
 
ret = file_type[i].load(argc, argv, kernel_buf, kernel_size, &info);
if (ret < 0) {
fprintf(stderr, "Cannot load %s\n", kernel);
+   close(kernel_fd);
return ret;
}
 
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2] kexec: Clean up kexec_calculate_store_digests() in kexec_file.c

2020-08-15 Thread Youling Tang
Change a couple of breaks to gotos, remove multiple unnecessary checks.

Signed-off-by: Youling Tang 
Acked-by: Dave Young 
Suggested-by: Joe Perches 
---
 kernel/kexec_file.c | 30 --
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 78c0837..ec8b716 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -763,7 +763,7 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
ret = crypto_shash_update(desc, ksegment->kbuf,
  ksegment->bufsz);
if (ret)
-   break;
+   goto out_free_digest;
 
/*
 * Assume rest of the buffer is filled with zero and
@@ -777,32 +777,26 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
bytes = zero_buf_sz;
ret = crypto_shash_update(desc, zero_buf, bytes);
if (ret)
-   break;
+   goto out_free_digest;
nullsz -= bytes;
}
 
-   if (ret)
-   break;
-
sha_regions[j].start = ksegment->mem;
sha_regions[j].len = ksegment->memsz;
j++;
}
 
-   if (!ret) {
-   ret = crypto_shash_final(desc, digest);
-   if (ret)
-   goto out_free_digest;
-   ret = kexec_purgatory_get_set_symbol(image, 
"purgatory_sha_regions",
-sha_regions, 
sha_region_sz, 0);
-   if (ret)
-   goto out_free_digest;
+   ret = crypto_shash_final(desc, digest);
+   if (ret)
+   goto out_free_digest;
 
-   ret = kexec_purgatory_get_set_symbol(image, 
"purgatory_sha256_digest",
-digest, 
SHA256_DIGEST_SIZE, 0);
-   if (ret)
-   goto out_free_digest;
-   }
+   ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
+sha_regions, sha_region_sz, 0);
+   if (ret)
+   goto out_free_digest;
+
+   ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
+digest, SHA256_DIGEST_SIZE, 0);
 
 out_free_digest:
kfree(digest);
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH v2] kexec: Clean up kexec_calculate_store_digests() in kexec_file.c

2020-08-14 Thread Youling Tang
Change a couple of breaks to gotos, remove multiple unnecessary checks.

Signed-off-by: Youling Tang 
Acked-by: Dave Young 
Suggested-by: Joe Perches 
---
 kernel/kexec_file.c | 30 --
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 78c0837..ec8b716 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -763,7 +763,7 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
ret = crypto_shash_update(desc, ksegment->kbuf,
  ksegment->bufsz);
if (ret)
-   break;
+   goto out_free_digest;
 
/*
 * Assume rest of the buffer is filled with zero and
@@ -777,32 +777,26 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
bytes = zero_buf_sz;
ret = crypto_shash_update(desc, zero_buf, bytes);
if (ret)
-   break;
+   goto out_free_digest;
nullsz -= bytes;
}
 
-   if (ret)
-   break;
-
sha_regions[j].start = ksegment->mem;
sha_regions[j].len = ksegment->memsz;
j++;
}
 
-   if (!ret) {
-   ret = crypto_shash_final(desc, digest);
-   if (ret)
-   goto out_free_digest;
-   ret = kexec_purgatory_get_set_symbol(image, 
"purgatory_sha_regions",
-sha_regions, 
sha_region_sz, 0);
-   if (ret)
-   goto out_free_digest;
+   ret = crypto_shash_final(desc, digest);
+   if (ret)
+   goto out_free_digest;
 
-   ret = kexec_purgatory_get_set_symbol(image, 
"purgatory_sha256_digest",
-digest, 
SHA256_DIGEST_SIZE, 0);
-   if (ret)
-   goto out_free_digest;
-   }
+   ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
+sha_regions, sha_region_sz, 0);
+   if (ret)
+   goto out_free_digest;
+
+   ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
+digest, SHA256_DIGEST_SIZE, 0);
 
 out_free_digest:
kfree(digest);
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH] kexec: Delete an unnecessary comparison

2020-08-14 Thread Youling Tang




On 08/14/2020 02:07 PM, Joe Perches wrote:

On Thu, 2020-08-13 at 20:45 +0800, Youling Tang wrote:

Regardless of whether the ret value is zero or non-zero, the trajectory
of the program execution is the same, so there is no need to compare.

Signed-off-by: Youling Tang 
---
  kernel/kexec_file.c | 2 --
  1 file changed, 2 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 78c0837..3ad0ae2 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -800,8 +800,6 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
  
  		ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",

 digest, 
SHA256_DIGEST_SIZE, 0);
-   if (ret)
-   goto out_free_digest;
}
  
  out_free_digest:

If you really want to change the function, then
you could change a couple of breaks to gotos,
remove multiple unnecessary tests, and unindent
a block of code too.

---
  kernel/kexec_file.c | 30 --
  1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index ca40bef75a61..34a025e85887 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -763,7 +763,7 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
ret = crypto_shash_update(desc, ksegment->kbuf,
  ksegment->bufsz);
if (ret)
-   break;
+   goto out_free_digest;
  
  		/*

 * Assume rest of the buffer is filled with zero and
@@ -777,32 +777,26 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
bytes = zero_buf_sz;
ret = crypto_shash_update(desc, zero_buf, bytes);
if (ret)
-   break;
+   goto out_free_digest;
nullsz -= bytes;
}
  
-		if (ret)

-   break;
-
sha_regions[j].start = ksegment->mem;
sha_regions[j].len = ksegment->memsz;
j++;
}
  
-	if (!ret) {

-   ret = crypto_shash_final(desc, digest);
-   if (ret)
-   goto out_free_digest;
-   ret = kexec_purgatory_get_set_symbol(image, 
"purgatory_sha_regions",
-sha_regions, 
sha_region_sz, 0);
-   if (ret)
-   goto out_free_digest;
+   ret = crypto_shash_final(desc, digest);
+   if (ret)
+   goto out_free_digest;
  
-		ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",

-digest, 
SHA256_DIGEST_SIZE, 0);
-   if (ret)
-   goto out_free_digest;
-   }
+   ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha_regions",
+sha_regions, sha_region_sz, 0);
+   if (ret)
+   goto out_free_digest;
+
+   ret = kexec_purgatory_get_set_symbol(image, "purgatory_sha256_digest",
+digest, SHA256_DIGEST_SIZE, 0);
  
  out_free_digest:

kfree(digest);


OK, looks good to me, I will send v2.

Thanks,
Youling


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] kexec: Delete an unnecessary comparison

2020-08-13 Thread Youling Tang
Regardless of whether the ret value is zero or non-zero, the trajectory
of the program execution is the same, so there is no need to compare.

Signed-off-by: Youling Tang 
---
 kernel/kexec_file.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 78c0837..3ad0ae2 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -800,8 +800,6 @@ static int kexec_calculate_store_digests(struct kimage 
*image)
 
ret = kexec_purgatory_get_set_symbol(image, 
"purgatory_sha256_digest",
 digest, 
SHA256_DIGEST_SIZE, 0);
-   if (ret)
-   goto out_free_digest;
}
 
 out_free_digest:
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH] MIPS: Fix compile warnnings in kexec-elf-mips.c

2020-08-12 Thread Youling Tang
Fix the following warnings:

kexec/arch/mips/kexec-elf-mips.c:161:41: warning: passing argument 3 of
‘dtb_set_initrd’ makes integer from pointer without a cast
dtb_set_initrd(&dtb_buf, &dtb_length, initrd_buf, initrd_buf + initrd_size);
 ^
In file included from kexec/arch/mips/kexec-elf-mips.c:33:0:
kexec/arch/mips/../../dt-ops.h:6:5: note: expected ‘off_t’ but argument is
of type ‘char *’
int dtb_set_initrd(char **dtb, off_t *dtb_size, off_t start, off_t end);
 ^

kexec/arch/mips/kexec-elf-mips.c:161:53: warning: passing argument 4 of
‘dtb_set_initrd’ makes integer from pointer without a cast
dtb_set_initrd(&dtb_buf, &dtb_length, initrd_buf, initrd_buf + initrd_size);
 ^
In file included from kexec/arch/mips/kexec-elf-mips.c:33:0:
kexec/arch/mips/../../dt-ops.h:6:5: note: expected ‘off_t’ but argument is
of type ‘char *’
int dtb_set_initrd(char **dtb, off_t *dtb_size, off_t start, off_t end);
 ^

Signed-off-by: Youling Tang 
---
 kexec/arch/mips/kexec-elf-mips.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kexec/arch/mips/kexec-elf-mips.c b/kexec/arch/mips/kexec-elf-mips.c
index cf90879..a2d11fc 100644
--- a/kexec/arch/mips/kexec-elf-mips.c
+++ b/kexec/arch/mips/kexec-elf-mips.c
@@ -161,7 +161,7 @@ int elf_mips_load(int argc, char **argv, const char *buf, 
off_t len,
 
/* Create initrd entries in dtb - although at this time
 * they would not point to the correct location */
-   dtb_set_initrd(&dtb_buf, &dtb_length, initrd_buf, initrd_buf + 
initrd_size);
+   dtb_set_initrd(&dtb_buf, &dtb_length, (off_t)initrd_buf, 
(off_t)initrd_buf + initrd_size);
 
initrd_base = add_buffer(info, initrd_buf, initrd_size,
initrd_size, sizeof(void *),
-- 
2.1.0


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec