Re: [PATCH v1 05/10] MIPS: add kexec ELF loading support

2018-05-17 Thread Sascha Hauer
On Thu, May 17, 2018 at 12:07:29PM +0200, Oleksij Rempel wrote:
> Am 17.05.2018 um 11:56 schrieb Sascha Hauer:
> > On Wed, May 16, 2018 at 06:42:28PM +0200, Oleksij Rempel wrote:
> >> From: Antony Pavlov 
> >>
> >> Signed-off-by: Antony Pavlov 
> >> Signed-off-by: Peter Mamonov 
> >> ---
> >>  arch/mips/include/asm/elf.h|   8 +-
> >>  arch/mips/lib/Makefile |   4 +
> >>  arch/mips/lib/kexec-mach-generic.c |  21 +++
> >>  arch/mips/lib/kexec-mips.c | 307 
> >> +
> >>  arch/mips/lib/machine_kexec.h  |  21 +++
> >>  arch/mips/lib/relocate_kernel.S| 108 +
> >>  6 files changed, 468 insertions(+), 1 deletion(-)
> >>  create mode 100644 arch/mips/lib/kexec-mach-generic.c
> >>  create mode 100644 arch/mips/lib/kexec-mips.c
> >>  create mode 100644 arch/mips/lib/machine_kexec.h
> >>  create mode 100644 arch/mips/lib/relocate_kernel.S
> >>
> >> +int kexec_load(struct image_data *data, void *entry,
> >> + unsigned long nr_segments,
> >> + struct kexec_segment *segments)
> >> +{
> >> +  int i;
> >> +  struct resource *elf;
> >> +  resource_size_t start;
> >> +  LIST_HEAD(elf_segments);
> >> +
> >> +  for (i = 0; i < nr_segments; i++) {
> >> +  resource_size_t mem = (resource_size_t)segments[i].mem;
> >> +
> >> +  elf = create_resource("elf segment",
> >> +  mem, mem + segments[i].memsz - 1);
> >> +
> >> +  list_add_used_region(>sibling, _segments);
> >> +  }
> >> +
> >> +  if (check_room_for_elf(_segments)) {
> >> +  pr_err("ELF can't be loaded!\n");
> >> +  return -ENOSPC;
> >> +  }
> >> +
> >> +  start = dcheck_res(_segments);
> >> +
> >> +  /* relocate_new_kernel() copy by register (4 or 8 bytes)
> >> + so start address must be aligned to 4/8 */
> >> +  start = (start + 15) & 0xfff0;
> >> +
> >> +  for (i = 0; i < nr_segments; i++) {
> >> +  segments[i].mem = (void *)(phys_to_virt((unsigned 
> >> long)segments[i].mem));
> >> +  memcpy(phys_to_virt(start), segments[i].buf, segments[i].bufsz);
> >> +  request_sdram_region("kexec relocatable segment",
> >> +  (unsigned long)phys_to_virt(start),
> >> +  (unsigned long)segments[i].bufsz);
> > 
> > The return value of request_sdram_region should be checked and of course
> > the resource should be used after having checked the return value, not
> > before. Same for the other calls to request_sdram_region.
> 
> Will it be enough for initial inclusion?

No, to merge it I need the feeling that this code is understood, not in
the works-for-me state.

Sascha

-- 
Pengutronix e.K.   | |
Industrial Linux Solutions | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

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


Re: [PATCH v1 05/10] MIPS: add kexec ELF loading support

2018-05-17 Thread Oleksij Rempel
Am 17.05.2018 um 11:56 schrieb Sascha Hauer:
> On Wed, May 16, 2018 at 06:42:28PM +0200, Oleksij Rempel wrote:
>> From: Antony Pavlov 
>>
>> Signed-off-by: Antony Pavlov 
>> Signed-off-by: Peter Mamonov 
>> ---
>>  arch/mips/include/asm/elf.h|   8 +-
>>  arch/mips/lib/Makefile |   4 +
>>  arch/mips/lib/kexec-mach-generic.c |  21 +++
>>  arch/mips/lib/kexec-mips.c | 307 
>> +
>>  arch/mips/lib/machine_kexec.h  |  21 +++
>>  arch/mips/lib/relocate_kernel.S| 108 +
>>  6 files changed, 468 insertions(+), 1 deletion(-)
>>  create mode 100644 arch/mips/lib/kexec-mach-generic.c
>>  create mode 100644 arch/mips/lib/kexec-mips.c
>>  create mode 100644 arch/mips/lib/machine_kexec.h
>>  create mode 100644 arch/mips/lib/relocate_kernel.S
>>
>> +int kexec_load(struct image_data *data, void *entry,
>> +   unsigned long nr_segments,
>> +   struct kexec_segment *segments)
>> +{
>> +int i;
>> +struct resource *elf;
>> +resource_size_t start;
>> +LIST_HEAD(elf_segments);
>> +
>> +for (i = 0; i < nr_segments; i++) {
>> +resource_size_t mem = (resource_size_t)segments[i].mem;
>> +
>> +elf = create_resource("elf segment",
>> +mem, mem + segments[i].memsz - 1);
>> +
>> +list_add_used_region(>sibling, _segments);
>> +}
>> +
>> +if (check_room_for_elf(_segments)) {
>> +pr_err("ELF can't be loaded!\n");
>> +return -ENOSPC;
>> +}
>> +
>> +start = dcheck_res(_segments);
>> +
>> +/* relocate_new_kernel() copy by register (4 or 8 bytes)
>> +   so start address must be aligned to 4/8 */
>> +start = (start + 15) & 0xfff0;
>> +
>> +for (i = 0; i < nr_segments; i++) {
>> +segments[i].mem = (void *)(phys_to_virt((unsigned 
>> long)segments[i].mem));
>> +memcpy(phys_to_virt(start), segments[i].buf, segments[i].bufsz);
>> +request_sdram_region("kexec relocatable segment",
>> +(unsigned long)phys_to_virt(start),
>> +(unsigned long)segments[i].bufsz);
> 
> The return value of request_sdram_region should be checked and of course
> the resource should be used after having checked the return value, not
> before. Same for the other calls to request_sdram_region.

Will it be enough for initial inclusion?

-- 
Regards,
Oleksij



signature.asc
Description: OpenPGP digital signature
___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH v1 05/10] MIPS: add kexec ELF loading support

2018-05-17 Thread Sascha Hauer
On Wed, May 16, 2018 at 06:42:28PM +0200, Oleksij Rempel wrote:
> From: Antony Pavlov 
> 
> Signed-off-by: Antony Pavlov 
> Signed-off-by: Peter Mamonov 
> ---
>  arch/mips/include/asm/elf.h|   8 +-
>  arch/mips/lib/Makefile |   4 +
>  arch/mips/lib/kexec-mach-generic.c |  21 +++
>  arch/mips/lib/kexec-mips.c | 307 
> +
>  arch/mips/lib/machine_kexec.h  |  21 +++
>  arch/mips/lib/relocate_kernel.S| 108 +
>  6 files changed, 468 insertions(+), 1 deletion(-)
>  create mode 100644 arch/mips/lib/kexec-mach-generic.c
>  create mode 100644 arch/mips/lib/kexec-mips.c
>  create mode 100644 arch/mips/lib/machine_kexec.h
>  create mode 100644 arch/mips/lib/relocate_kernel.S
> 
> +int kexec_load(struct image_data *data, void *entry,
> +unsigned long nr_segments,
> +struct kexec_segment *segments)
> +{
> + int i;
> + struct resource *elf;
> + resource_size_t start;
> + LIST_HEAD(elf_segments);
> +
> + for (i = 0; i < nr_segments; i++) {
> + resource_size_t mem = (resource_size_t)segments[i].mem;
> +
> + elf = create_resource("elf segment",
> + mem, mem + segments[i].memsz - 1);
> +
> + list_add_used_region(>sibling, _segments);
> + }
> +
> + if (check_room_for_elf(_segments)) {
> + pr_err("ELF can't be loaded!\n");
> + return -ENOSPC;
> + }
> +
> + start = dcheck_res(_segments);
> +
> + /* relocate_new_kernel() copy by register (4 or 8 bytes)
> +so start address must be aligned to 4/8 */
> + start = (start + 15) & 0xfff0;
> +
> + for (i = 0; i < nr_segments; i++) {
> + segments[i].mem = (void *)(phys_to_virt((unsigned 
> long)segments[i].mem));
> + memcpy(phys_to_virt(start), segments[i].buf, segments[i].bufsz);
> + request_sdram_region("kexec relocatable segment",
> + (unsigned long)phys_to_virt(start),
> + (unsigned long)segments[i].bufsz);

The return value of request_sdram_region should be checked and of course
the resource should be used after having checked the return value, not
before. Same for the other calls to request_sdram_region.

Sascha

-- 
Pengutronix e.K.   | |
Industrial Linux Solutions | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0|
Amtsgericht Hildesheim, HRA 2686   | Fax:   +49-5121-206917- |

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


[PATCH v1 05/10] MIPS: add kexec ELF loading support

2018-05-16 Thread Oleksij Rempel
From: Antony Pavlov 

Signed-off-by: Antony Pavlov 
Signed-off-by: Peter Mamonov 
---
 arch/mips/include/asm/elf.h|   8 +-
 arch/mips/lib/Makefile |   4 +
 arch/mips/lib/kexec-mach-generic.c |  21 +++
 arch/mips/lib/kexec-mips.c | 307 +
 arch/mips/lib/machine_kexec.h  |  21 +++
 arch/mips/lib/relocate_kernel.S| 108 +
 6 files changed, 468 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/lib/kexec-mach-generic.c
 create mode 100644 arch/mips/lib/kexec-mips.c
 create mode 100644 arch/mips/lib/machine_kexec.h
 create mode 100644 arch/mips/lib/relocate_kernel.S

diff --git a/arch/mips/include/asm/elf.h b/arch/mips/include/asm/elf.h
index b8b82191c..bf974f521 100644
--- a/arch/mips/include/asm/elf.h
+++ b/arch/mips/include/asm/elf.h
@@ -17,12 +17,18 @@
 
 #ifndef ELF_ARCH
 
+/* Legal values for e_machine (architecture).  */
+
+#define EM_MIPS 8  /* MIPS R3000 big-endian */
+#define EM_MIPS_RS4_BE 10  /* MIPS R4000 big-endian */
+
 #ifdef CONFIG_32BIT
 
 /*
  * This is used to ensure we don't load something for the wrong architecture.
  */
-#define elf_check_arch(hdr)\
+#define elf_check_arch(x) ((x)->e_machine == EM_MIPS)
+
 /*
  * These are used to set parameters in the core dumps.
  */
diff --git a/arch/mips/lib/Makefile b/arch/mips/lib/Makefile
index d25d0969f..29f6581b0 100644
--- a/arch/mips/lib/Makefile
+++ b/arch/mips/lib/Makefile
@@ -18,4 +18,8 @@ obj-$(CONFIG_CPU_MIPS64) += c-r4k.o
 obj-$(CONFIG_CMD_MIPS_CPUINFO) += cpuinfo.o
 obj-$(CONFIG_CMD_BOOTM)+= bootm.o
 
+obj-$(CONFIG_KEXEC) += kexec-mips.o
+obj-$(CONFIG_KEXEC) += relocate_kernel.o
+obj-$(CONFIG_KEXEC) += kexec-mach-generic.o
+
 pbl-y  += ashldi3.o
diff --git a/arch/mips/lib/kexec-mach-generic.c 
b/arch/mips/lib/kexec-mach-generic.c
new file mode 100644
index 0..def38cbc0
--- /dev/null
+++ b/arch/mips/lib/kexec-mach-generic.c
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018 Antony Pavlov 
+ */
+
+#include 
+#include 
+#include 
+
+void kexec_arch(void *opaque)
+{
+   extern unsigned long reboot_code_buffer;
+   void (*kexec_code_buffer)(void);
+
+   shutdown_barebox();
+
+   kexec_code_buffer = phys_to_virt(reboot_code_buffer);
+
+   kexec_code_buffer();
+}
+EXPORT_SYMBOL(kexec_arch);
diff --git a/arch/mips/lib/kexec-mips.c b/arch/mips/lib/kexec-mips.c
new file mode 100644
index 0..781552f01
--- /dev/null
+++ b/arch/mips/lib/kexec-mips.c
@@ -0,0 +1,307 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * kexec-mips.c - kexec for mips
+ * Copyright (C) 2007 Francesco Chiechi, Alessandro Rubini
+ * Copyright (C) 2007 Tvblob s.r.l.
+ *
+ * derived from ../ppc/kexec-mips.c
+ * Copyright (C) 2004, 2005 Albert Herranz
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "machine_kexec.h"
+
+static unsigned int mips_boot_protocol;
+
+enum mips_boot_names {
+   MIPS_BOOT_FDT,
+   MIPS_BOOT_LEGACY,
+};
+
+static int elf_mips_probe(const char *buf, off_t len)
+{
+   struct mem_ehdr ehdr;
+   int ret;
+
+   ret = build_elf_exec_info(buf, len, );
+   if (IS_ERR_VALUE(ret)) {
+   goto out;
+   }
+
+   if (ehdr.e_machine != EM_MIPS) {
+   pr_err("Not for this architecture.\n");
+   ret = -EFAULT;
+   goto out;
+   }
+
+ out:
+   free_elf_info();
+
+   return ret;
+}
+
+static int elf_mips_load(const char *buf, off_t len, struct kexec_info *info)
+{
+   struct mem_ehdr ehdr;
+   int ret;
+   size_t i;
+
+   ret = build_elf_exec_info(buf, len, );
+   if (IS_ERR_VALUE(ret)) {
+   pr_err("ELF exec parse failed\n");
+   goto out;
+   }
+
+   /* Read in the PT_LOAD segments and remove CKSEG0 mask from address */
+   for (i = 0; i < ehdr.e_phnum; i++) {
+   struct mem_phdr *phdr;
+   phdr = _phdr[i];
+   if (phdr->p_type == PT_LOAD) {
+   phdr->p_paddr = virt_to_phys((const void 
*)phdr->p_paddr);
+   }
+   }
+
+   /* Load the ELF data */
+   ret = elf_exec_load(, info);
+   if (IS_ERR_VALUE(ret)) {
+   pr_err("ELF exec load failed\n");
+   goto out;
+   }
+
+   info->entry = (void *)virt_to_phys((void *)ehdr.e_entry);
+
+out:
+   return ret;
+}
+
+struct kexec_file_type kexec_file_type[] = {
+   {"elf-mips", elf_mips_probe, elf_mips_load },
+};
+int kexec_file_types = sizeof(kexec_file_type) / sizeof(kexec_file_type[0]);
+
+/*
+ * add_segment() should convert base to a physical address on mips,
+ * while the default is just to work with base as is */
+void add_segment(struct kexec_info *info,