Skip to contentSkip to conversation

Support separate -firmware and -kernel

PATCH In review Stefan O'Rear 2 participants 9 messages Dec 16, 2018 – Dec 17, 2018 RISC-V

lore
Share & tools
lore
#0 lore raw

v2 cover Support separate -firmware and -kernel

This was posted in May[0] by Michael Clark. I have split it up to address earlier review feedback, along with other minor cleanups.

[0]: http://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg05121.html

Stefan O'Rear (4):
RISC-V: Move firmware loading logic to a separate file
RISC-V: refactor initrd loading
RISC-V: Honor entry point in loaded ELF firmware
RISC-V: Support separate firmware and kernel payload

 hw/riscv/Makefile.objs  |   1 +
 hw/riscv/boot.c         | 170 ++++++++++++++++++++++++++++++++++++++++++++++++
 hw/riscv/virt.c         |  69 ++++----------------
 include/hw/riscv/boot.h |  24 +++++++
 4 files changed, 208 insertions(+), 56 deletions(-)
 create mode 100644 hw/riscv/boot.c
 create mode 100644 include/hw/riscv/boot.h
Signature · 1 line
2.8.0
#1 lore raw

PATCH v2 1/4 RISC-V: Move firmware loading logic to a separate file

---
 hw/riscv/Makefile.objs  |  1 +
 hw/riscv/boot.c         | 85 +++++++++++++++++++++++++++++++++++++++++++++++++
 hw/riscv/virt.c         | 58 ++-------------------------------
 include/hw/riscv/boot.h | 24 ++++++++++++++
 4 files changed, 112 insertions(+), 56 deletions(-)
 create mode 100644 hw/riscv/boot.c
 create mode 100644 include/hw/riscv/boot.h
hw/riscv/Makefile.objs +1 -0
index 1dde01d..d36b004 100644
@@ -1,3 +1,4 @@
+obj-y += boot.o
 obj-y += riscv_htif.o
 obj-y += riscv_hart.o
 obj-y += sifive_e.o
hw/riscv/boot.c new file +85 -0

85 lines not on this page · raw message

hw/riscv/virt.c +2 -56

76 lines not on this page · raw message

include/hw/riscv/boot.h new file +24 -0
new file mode 100644
index 0000000..92948f3
@@ -0,0 +1,24 @@
+/*
+ * QEMU RISCV firmware and kernel loader interface
+ *
+ * Copyright (c) 2017-2018 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2 or later, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef HW_RISCV_BOOT_H
+#define HW_RISCV_BOOT_H
+
+void riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt);
+
+#endif
Signature · 1 line
2.8.0
#2 lore raw

Re: [Qemu-riscv] [PATCH v2 1/4] RISC-V: Move firmware loading logic to a separate file

Quoted 50 lines · Stefan O'Rear
---
 hw/riscv/Makefile.objs  |  1 +
 hw/riscv/boot.c         | 85
+++++++++++++++++++++++++++++++++++++++++++++++++
 hw/riscv/virt.c         | 58 ++-------------------------------
 include/hw/riscv/boot.h | 24 ++++++++++++++
 4 files changed, 112 insertions(+), 56 deletions(-)
 create mode 100644 hw/riscv/boot.c
 create mode 100644 include/hw/riscv/boot.h
hw/riscv/Makefile.objs +1 -0
index 1dde01d..d36b004 100644
@@ -1,3 +1,4 @@
+obj-y += boot.o
 obj-y += riscv_htif.o
 obj-y += riscv_hart.o
 obj-y += sifive_e.o
hw/riscv/boot.c new file +6 -0
new file mode 100644
index 0000000..1e4d017
@@ -0,0 +1,85 @@
+/*
+ * QEMU RISCV firmware and kernel loader
+ *
+ * Copyright (c) 2017-2018 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or

modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2 or later, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>;.
+ */

hw/riscv/boot.c new file +2 -0
+
+#include "qemu/osdep.h"

hw/riscv/boot.c line 29

Quoted 10 lines
hw/riscv/boot.c new file +10 -0
+#include "qemu/units.h"
+#include "qemu/error-report.h"
+#include "hw/loader.h"
+#include "hw/boards.h"
+#include "sysemu/device_tree.h"
+#include "elf.h"
+#include "hw/riscv/boot.h"
+
+static uint64_t load_kernel(const char *kernel_filename)
+{

This is going to conflict with this patch:

https://patchwork.kernel.org/patch/10701667/

It'll just be a simple change though.

Alistair

Quoted 196 lines
hw/riscv/boot.c new file +18 -0
+    uint64_t kernel_entry, kernel_high;
+
+    if (load_elf(kernel_filename, NULL, NULL,
+                 &kernel_entry, NULL, &kernel_high,
+                 0, EM_RISCV, 1, 0) < 0) {
+        error_report("could not load kernel '%s'", kernel_filename);
+        exit(1);
+    }
+    return kernel_entry;
+}
+
+static hwaddr load_initrd(const char *filename, uint64_t mem_size,
+                          uint64_t kernel_entry, hwaddr *start)
+{
+    int size;
+
+    /* We want to put the initrd far enough into RAM that when the
+     * kernel is uncompressed it will not clobber the initrd.

However

hw/riscv/boot.c new file +2 -0
+     * on boards without much RAM we must ensure that we still leave
+     * enough room for a decent sized initrd, and on boards with

large
+ * amounts of RAM we must avoid the initrd being so far up in
RAM

hw/riscv/boot.c new file +3 -0
+     * that it is outside lowmem and inaccessible to the kernel.
+     * So for boards with less  than 256MB of RAM we put the initrd
+     * halfway into RAM, and for boards with 256MB of RAM or more we

put

hw/riscv/boot.c new file +7 -0
+     * the initrd at 128MB.
+     */
+    *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
+
+    size = load_ramdisk(filename, *start, mem_size - *start);
+    if (size == -1) {
+        size = load_image_targphys(filename, *start, mem_size -

*start);

hw/riscv/boot.c new file +9 -0
+        if (size == -1) {
+            error_report("could not load ramdisk '%s'", filename);
+            exit(1);
+        }
+    }
+    return *start + size;
+}
+
+void riscv_load_firmware_kernel_initrd(MachineState *machine, void

*fdt)

hw/riscv/boot.c new file +3 -0
+{
+    if (machine->kernel_filename) {
+        uint64_t kernel_entry = load_kernel(machine-
Quoted 1 line

kernel_filename);

hw/riscv/boot.c new file +5 -0
+
+        if (machine->initrd_filename) {
+            hwaddr start;
+            hwaddr end = load_initrd(machine->initrd_filename,
+                                     machine->ram_size,

kernel_entry,

hw/riscv/boot.c new file +4 -0
+                                     &start);
+            qemu_fdt_setprop_cell(fdt, "/chosen",
+                                  "linux,initrd-start", start);
+            qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-

end",

hw/riscv/boot.c new file +4 -0
+                                  end);
+        }
+    }
+}
hw/riscv/virt.c +1 -20
index 2b38f89..4c2e894 100644
@@ -34,6 +34,7 @@
 #include "hw/riscv/sifive_plic.h"
 #include "hw/riscv/sifive_clint.h"
 #include "hw/riscv/sifive_test.h"
+#include "hw/riscv/boot.h"
 #include "hw/riscv/virt.h"
 #include "chardev/char.h"
 #include "sysemu/arch_init.h"
@@ -57,47 +58,6 @@ static const struct MemmapEntry {
     [VIRT_DRAM] =     { 0x80000000,        0x0 },
 };
 
-static uint64_t load_kernel(const char *kernel_filename)
-{
-    uint64_t kernel_entry, kernel_high;
-
-    if (load_elf(kernel_filename, NULL, NULL,
-                 &kernel_entry, NULL, &kernel_high,
-                 0, EM_RISCV, 1, 0) < 0) {
-        error_report("could not load kernel '%s'", kernel_filename);
-        exit(1);
-    }
-    return kernel_entry;
-}
-
-static hwaddr load_initrd(const char *filename, uint64_t mem_size,
-                          uint64_t kernel_entry, hwaddr *start)
-{
-    int size;
-
-    /* We want to put the initrd far enough into RAM that when the
-     * kernel is uncompressed it will not clobber the initrd.

However

hw/riscv/virt.c +0 -2
-     * on boards without much RAM we must ensure that we still leave
-     * enough room for a decent sized initrd, and on boards with

large
- * amounts of RAM we must avoid the initrd being so far up in
RAM

hw/riscv/virt.c +0 -3
-     * that it is outside lowmem and inaccessible to the kernel.
-     * So for boards with less  than 256MB of RAM we put the initrd
-     * halfway into RAM, and for boards with 256MB of RAM or more we

put

hw/riscv/virt.c +0 -7
-     * the initrd at 128MB.
-     */
-    *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
-
-    size = load_ramdisk(filename, *start, mem_size - *start);
-    if (size == -1) {
-        size = load_image_targphys(filename, *start, mem_size -

*start);

hw/riscv/virt.c +0 -8
-        if (size == -1) {
-            error_report("could not load ramdisk '%s'", filename);
-            exit(1);
-        }
-    }
-    return *start + size;
-}
-
 static void *create_fdt(RISCVVirtState *s, const struct MemmapEntry

*memmap,
uint64_t mem_size, const char *cmdline)
{

hw/riscv/virt.c +0 -11
@@ -301,21 +261,7 @@ static void riscv_virt_board_init(MachineState
 *machine)
     memory_region_add_subregion(system_memory,
 memmap[VIRT_MROM].base,
                                 mask_rom);
 
-    if (machine->kernel_filename) {
-        uint64_t kernel_entry = load_kernel(machine-
 >kernel_filename);
-
-        if (machine->initrd_filename) {
-            hwaddr start;
-            hwaddr end = load_initrd(machine->initrd_filename,
-                                     machine->ram_size,
 kernel_entry,
-                                     &start);
-            qemu_fdt_setprop_cell(fdt, "/chosen",
-                                  "linux,initrd-start", start);
-            qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-

end",

hw/riscv/virt.c +1 -4
-                                  end);
-        }
-    }
-
+    riscv_load_firmware_kernel_initrd(machine, fdt);
     /* reset vector */
     uint32_t reset_vec[8] = {
         0x00000297,                  /* 1:  auipc  t0,

%pcrel_hi(dtb) */

include/hw/riscv/boot.h new file +6 -0
new file mode 100644
index 0000000..92948f3
@@ -0,0 +1,24 @@
+/*
+ * QEMU RISCV firmware and kernel loader interface
+ *
+ * Copyright (c) 2017-2018 SiFive, Inc.
+ *
+ * This program is free software; you can redistribute it and/or

modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2 or later, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>;.
+ */

include/hw/riscv/boot.h new file +5 -0
+
+#ifndef HW_RISCV_BOOT_H
+#define HW_RISCV_BOOT_H
+
+void riscv_load_firmware_kernel_initrd(MachineState *machine, void

*fdt);

include/hw/riscv/boot.h new file +2 -0
+
+#endif
#3 lore raw

PATCH v2 2/4 RISC-V: refactor initrd loading

In reply to Stefan O'Rear
---
 hw/riscv/boot.c | 48 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 16 deletions(-)
hw/riscv/boot.c +32 -16
index 1e4d017..54e3139 100644
@@ -25,6 +25,13 @@
 #include "elf.h"
 #include "hw/riscv/boot.h"
 
+#define RISCV_BOOT_DEBUG 0
+
+#define boot_debug(fs, ...) \
+    if (RISCV_BOOT_DEBUG) { \
+        fprintf(stderr, "boot: %s: "fs, __func__, ##__VA_ARGS__); \
+    }
+
 static uint64_t load_kernel(const char *kernel_filename)
 {
     uint64_t kernel_entry, kernel_high;
@@ -38,10 +45,10 @@ static uint64_t load_kernel(const char *kernel_filename)
     return kernel_entry;
 }
 
-static hwaddr load_initrd(const char *filename, uint64_t mem_size,
-                          uint64_t kernel_entry, hwaddr *start)
+static void riscv_load_initrd(const char *filename, uint64_t mem_size,
+                              hwaddr firmware_entry, void *fdt)
 {
-    int size;
+    uint64_t start, size;
 
     /* We want to put the initrd far enough into RAM that when the
      * kernel is uncompressed it will not clobber the initrd. However
@@ -53,33 +60,42 @@ static hwaddr load_initrd(const char *filename, uint64_t mem_size,
      * halfway into RAM, and for boards with 256MB of RAM or more we put
      * the initrd at 128MB.
      */
-    *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
+    start = firmware_entry + MIN(mem_size / 2, 128 * MiB);
 
-    size = load_ramdisk(filename, *start, mem_size - *start);
+    size = load_ramdisk(filename, start, mem_size - start);
     if (size == -1) {
-        size = load_image_targphys(filename, *start, mem_size - *start);
+        size = load_image_targphys(filename, start, mem_size - start);
         if (size == -1) {
             error_report("could not load ramdisk '%s'", filename);
             exit(1);
         }
     }
-    return *start + size;
+
+    boot_debug("start=0x" TARGET_FMT_plx " end=0x" TARGET_FMT_plx "\n",
+               start, start + size);
+
+    /*
+     * pass initrd load address via device-tree to kernel
+     *
+     * linux-kernel reads the initrd address from device-tree
+     */
+    if (fdt) {
+        qemu_fdt_setprop_cells(fdt, "/chosen", "linux,initrd-end",
+                               (start + size) >> 32, start + size);
+        qemu_fdt_setprop_cells(fdt, "/chosen", "linux,initrd-start",
+                               start >> 32, start);
+    }
 }
 
 void riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt)
 {
     if (machine->kernel_filename) {
-        uint64_t kernel_entry = load_kernel(machine->kernel_filename);
+        uint64_t firmware_entry = load_kernel(machine->kernel_filename);
 
         if (machine->initrd_filename) {
-            hwaddr start;
-            hwaddr end = load_initrd(machine->initrd_filename,
-                                     machine->ram_size, kernel_entry,
-                                     &start);
-            qemu_fdt_setprop_cell(fdt, "/chosen",
-                                  "linux,initrd-start", start);
-            qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
-                                  end);
+            /* load separate initrd */
+            riscv_load_initrd(machine->initrd_filename, machine->ram_size,
+                              firmware_entry, fdt);
         }
     }
 }
Signature · 1 line
2.8.0
#4 lore raw

Re: [Qemu-riscv] [PATCH v2 2/4] RISC-V: refactor initrd loading

Quoted 12 lines · Stefan O'Rear
---
 hw/riscv/boot.c | 48 ++++++++++++++++++++++++++++++++---------------
(quoted diff) +0 -1
-
 1 file changed, 32 insertions(+), 16 deletions(-)
hw/riscv/boot.c +0 -0
index 1e4d017..54e3139 100644

hw/riscv/boot.c line 32

Quoted 8 lines
hw/riscv/boot.c +5 -0
@@ -25,6 +25,13 @@
 #include "elf.h"
 #include "hw/riscv/boot.h"
 
+#define RISCV_BOOT_DEBUG 0
+
+#define boot_debug(fs, ...) \
+    if (RISCV_BOOT_DEBUG) { \
+        fprintf(stderr, "boot: %s: "fs, __func__, ##__VA_ARGS__); \

This should be using qemu_log() instead of fprintf().

Quoted 8 lines
hw/riscv/boot.c +2 -0
+    }
+
 static uint64_t load_kernel(const char *kernel_filename)
 {
     uint64_t kernel_entry, kernel_high;
@@ -38,10 +45,10 @@ static uint64_t load_kernel(const char
 *kernel_filename)
     return kernel_entry;

hw/riscv/boot.c static uint64_t load_kernel(const char · line 53

Quoted 10 lines
hw/riscv/boot.c +3 -3
 }
 
-static hwaddr load_initrd(const char *filename, uint64_t mem_size,
-                          uint64_t kernel_entry, hwaddr *start)
+static void riscv_load_initrd(const char *filename, uint64_t
 mem_size,
+                              hwaddr firmware_entry, void *fdt)
 {
-    int size;
+    uint64_t start, size;

start will have to depend on the bitlength of the target.

Alistair

Quoted 71 lines
hw/riscv/boot.c +0 -0
     /* We want to put the initrd far enough into RAM that when the

* kernel is uncompressed it will not clobber the initrd.
However

hw/riscv/boot.c +0 -0
@@ -53,33 +60,42 @@ static hwaddr load_initrd(const char *filename,
 uint64_t mem_size,
      * halfway into RAM, and for boards with 256MB of RAM or more we

put
* the initrd at 128MB.
*/

hw/riscv/boot.c +2 -3
-    *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
+    start = firmware_entry + MIN(mem_size / 2, 128 * MiB);
 
-    size = load_ramdisk(filename, *start, mem_size - *start);
+    size = load_ramdisk(filename, start, mem_size - start);
     if (size == -1) {
-        size = load_image_targphys(filename, *start, mem_size -

*start);
+ size = load_image_targphys(filename, start, mem_size -

start);
         if (size == -1) {
             error_report("could not load ramdisk '%s'", filename);
             exit(1);
         }
     }
hw/riscv/boot.c +2 -1
-    return *start + size;
+
+    boot_debug("start=0x" TARGET_FMT_plx " end=0x" TARGET_FMT_plx

"\n",

hw/riscv/boot.c +13 -0
+               start, start + size);
+
+    /*
+     * pass initrd load address via device-tree to kernel
+     *
+     * linux-kernel reads the initrd address from device-tree
+     */
+    if (fdt) {
+        qemu_fdt_setprop_cells(fdt, "/chosen", "linux,initrd-end",
+                               (start + size) >> 32, start + size);
+        qemu_fdt_setprop_cells(fdt, "/chosen", "linux,initrd-start",
+                               start >> 32, start);
+    }
 }
 
 void riscv_load_firmware_kernel_initrd(MachineState *machine, void

*fdt)

 {
     if (machine->kernel_filename) {

- uint64_t kernel_entry = load_kernel(machine-

Quoted 1 line

kernel_filename);

+ uint64_t firmware_entry = load_kernel(machine-

Quoted 1 line

kernel_filename);

         if (machine->initrd_filename) {
hw/riscv/boot.c +0 -3
-            hwaddr start;
-            hwaddr end = load_initrd(machine->initrd_filename,
-                                     machine->ram_size,

kernel_entry,

hw/riscv/boot.c +0 -4
-                                     &start);
-            qemu_fdt_setprop_cell(fdt, "/chosen",
-                                  "linux,initrd-start", start);
-            qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-

end",

hw/riscv/boot.c +2 -1
-                                  end);
+            /* load separate initrd */
+            riscv_load_initrd(machine->initrd_filename, machine-
Quoted 1 line

ram_size,

+                              firmware_entry, fdt);
         }
     }

}

#5 lore raw

PATCH v2 3/4 RISC-V: Honor entry point in loaded ELF firmware

In reply to Stefan O'Rear

The virt board is changed to use the firmware address from the loaded firmware or combined firmware+kernel. This is normally equal to the DRAM base address of 0x8000_0000, however now it is possible to boot firmware at different load addresses because the reset code jumps to the actual firmware entry address.

---
 hw/riscv/boot.c         |  9 ++++++---
 hw/riscv/virt.c         | 17 ++++++++++++++---
 include/hw/riscv/boot.h |  2 +-
 3 files changed, 21 insertions(+), 7 deletions(-)
hw/riscv/boot.c +6 -3
index 54e3139..e51e7f9 100644
@@ -87,15 +87,18 @@ static void riscv_load_initrd(const char *filename, uint64_t mem_size,
     }
 }
 
-void riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt)
+hwaddr riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt)
 {
-    if (machine->kernel_filename) {
-        uint64_t firmware_entry = load_kernel(machine->kernel_filename);
+    hwaddr firmware_entry = 0;
 
+    if (machine->kernel_filename) {
+        firmware_entry = load_kernel(machine->kernel_filename);
         if (machine->initrd_filename) {
             /* load separate initrd */
             riscv_load_initrd(machine->initrd_filename, machine->ram_size,
                               firmware_entry, fdt);
         }
     }
+
+    return firmware_entry;
 }
hw/riscv/virt.c +14 -3
index 4c2e894..38979f7 100644
@@ -235,6 +235,7 @@ static void riscv_virt_board_init(MachineState *machine)
     size_t plic_hart_config_len;
     int i;
     void *fdt;
+    hwaddr firmware_entry;
 
     /* Initialize SOC */
     object_initialize_child(OBJECT(machine), "soc", &s->soc, sizeof(s->soc),
@@ -261,7 +262,17 @@ static void riscv_virt_board_init(MachineState *machine)
     memory_region_add_subregion(system_memory, memmap[VIRT_MROM].base,
                                 mask_rom);
 
-    riscv_load_firmware_kernel_initrd(machine, fdt);
+    /*
+     * combined firmware and kernel: -kernel bbl_vmlimux
+     * separate firmware and kernel: -bios bbl -kernel vmlinux
+     * firmware, kernel and ramdisk: -bios bbl -kernel vmlinux -initrd initramfs
+     */
+    firmware_entry = riscv_load_firmware_kernel_initrd(machine, fdt);
+
+    if (firmware_entry == 0) {
+        firmware_entry = memmap[VIRT_DRAM].base;
+    }
+
     /* reset vector */
     uint32_t reset_vec[8] = {
         0x00000297,                  /* 1:  auipc  t0, %pcrel_hi(dtb) */
@@ -274,8 +285,8 @@ static void riscv_virt_board_init(MachineState *machine)
 #endif
         0x00028067,                  /*     jr     t0 */
         0x00000000,
-        memmap[VIRT_DRAM].base,      /* start: .dword memmap[VIRT_DRAM].base */
-        0x00000000,
+        firmware_entry,              /* .word firmware_entry */
+        firmware_entry >> 32,
                                      /* dtb: */
     };
 
include/hw/riscv/boot.h +1 -1
index 92948f3..3af21fc 100644
@@ -19,6 +19,6 @@
 #ifndef HW_RISCV_BOOT_H
 #define HW_RISCV_BOOT_H
 
-void riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt);
+hwaddr riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt);
 
 #endif
Signature · 1 line
2.8.0
#6 lore raw

PATCH v2 4/4 RISC-V: Support separate firmware and kernel payload

In reply to Stefan O'Rear

Support for separate firmware and kernel payload is added by updating BBL to read optional preloaded kernel address attributes from device-tree using a similar mechanism to that used to pass init ramdisk addresses to linux kernel.

    chosen {
        riscv,kernel-start = <0x00000000 0x80200000>;
        riscv,kernel-end = <0x00000000 0x80590634>;
    };

These attributes are added by QEMU and read by BBL when combining -bios <firmware-image> and -kernel <kernel-image> options. e.g.

$ qemu-system-riscv64 -machine virt -bios bbl -kernel vmlinux

With this change, bbl can be compiled without --with-payload and the dummy payload alignment is altered to make the memory footprint of the firmware-only bbl smaller. The dummy payload message is updated to indicate the alternative load method.

This load method could also be supported by a first stage boot loader that reads seperate firmware and kernel from SPI flash. The main advantage of this new mechanism is that it eases kernel development by avoiding the riscv-pk packaging step after kernel builds, makes building per repository artefacts for CI simpler, and mimics bootloaders on other platforms that can load a kernel image file directly. Ultimately BBL should use an SPI driver to load the kernel image however this mechanism supports use cases such such as QEMU's -bios, -kernel and -initrd options following examples from other platforms that pass kernel entry to firmware via device-tree.

---
 hw/riscv/boot.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 72 insertions(+), 6 deletions(-)
hw/riscv/boot.c +72 -6
index e51e7f9..43f328d 100644
@@ -32,16 +32,70 @@
         fprintf(stderr, "boot: %s: "fs, __func__, ##__VA_ARGS__); \
     }
 
-static uint64_t load_kernel(const char *kernel_filename)
+static uint64_t kernel_offset;
+
+static uint64_t kernel_translate(void *opaque, uint64_t addr)
 {
-    uint64_t kernel_entry, kernel_high;
+    /* mask kernel virtual address and offset by load address */
+    if (kernel_offset) {
+        return (addr & 0x7fffffff) + kernel_offset;
+    } else {
+        return addr;
+    }
+}
 
-    if (load_elf(kernel_filename, NULL, NULL,
-                 &kernel_entry, NULL, &kernel_high,
+static hwaddr riscv_load_firmware(const char *filename)
+{
+    uint64_t firmware_entry, firmware_start, firmware_end;
+
+    if (load_elf(filename, NULL, NULL,
+                 &firmware_entry, &firmware_start, &firmware_end,
                  0, EM_RISCV, 1, 0) < 0) {
-        error_report("could not load kernel '%s'", kernel_filename);
+        error_report("could not load firmware '%s'", filename);
         exit(1);
     }
+
+    /* align kernel load address to the megapage after the firmware */
+#if defined(TARGET_RISCV32)
+    kernel_offset = (firmware_end + 0x3fffff) & ~0x3fffff;
+#else
+    kernel_offset = (firmware_end + 0x1fffff) & ~0x1fffff;
+#endif
+
+    boot_debug("entry=0x" TARGET_FMT_plx " start=0x" TARGET_FMT_plx " "
+               "end=0x" TARGET_FMT_plx " kernel_offset=0x" TARGET_FMT_plx "\n",
+               firmware_entry, firmware_start, firmware_end, kernel_offset);
+
+    return firmware_entry;
+}
+
+static hwaddr riscv_load_kernel(const char *filename, void *fdt)
+{
+    uint64_t kernel_entry, kernel_start, kernel_end;
+
+    if (load_elf(filename, kernel_translate, NULL,
+                 &kernel_entry, &kernel_start, &kernel_end,
+                 0, EM_RISCV, 1, 0) < 0) {
+        error_report("could not load kernel '%s'", filename);
+        exit(1);
+    }
+
+    boot_debug("entry=0x" TARGET_FMT_plx " start=0x" TARGET_FMT_plx " "
+               "end=0x" TARGET_FMT_plx "\n", kernel_entry, kernel_start,
+               kernel_end);
+
+    /*
+     * pass kernel load address via device-tree to firmware
+     *
+     * BBL reads the kernel address from device-tree
+     */
+    if (fdt) {
+        qemu_fdt_setprop_cells(fdt, "/chosen", "riscv,kernel-end",
+                               kernel_end >> 32, kernel_end);
+        qemu_fdt_setprop_cells(fdt, "/chosen", "riscv,kernel-start",
+                               kernel_start >> 32, kernel_start);
+    }
+
     return kernel_entry;
 }
 
@@ -91,8 +145,20 @@ hwaddr riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt)
 {
     hwaddr firmware_entry = 0;
 
+    /* load firmware e.g. -bios bbl */
+    if (machine->firmware) {
+        firmware_entry = riscv_load_firmware(machine->firmware);
+    }
+
+    /* load combined bbl+kernel or separate kernel */
     if (machine->kernel_filename) {
-        firmware_entry = load_kernel(machine->kernel_filename);
+        if (machine->firmware) {
+            /* load separate bios and kernel e.g. -bios bbl -kernel vmlinux */
+            riscv_load_kernel(machine->kernel_filename, fdt);
+        } else {
+            /* load traditional combined bbl+kernel e.g. -kernel bbl_vmlimux */
+            firmware_entry = riscv_load_kernel(machine->kernel_filename, NULL);
+        }
         if (machine->initrd_filename) {
             /* load separate initrd */
             riscv_load_initrd(machine->initrd_filename, machine->ram_size,
Signature · 1 line
2.8.0
#7 lore raw

Re: [Qemu-riscv] [PATCH v2 4/4] RISC-V: Support separate firmware and kernel payload

Quoted 108 lines · Stefan O'Rear

Support for separate firmware and kernel payload is added by updating BBL to read optional preloaded kernel address attributes from device-tree using a similar mechanism to that used to pass init ramdisk addresses to linux kernel.

    chosen {
        riscv,kernel-start = <0x00000000 0x80200000>;
        riscv,kernel-end = <0x00000000 0x80590634>;
    };

These attributes are added by QEMU and read by BBL when combining -bios <firmware-image> and -kernel <kernel-image> options. e.g.

$ qemu-system-riscv64 -machine virt -bios bbl -kernel vmlinux

With this change, bbl can be compiled without --with-payload and the dummy payload alignment is altered to make the memory footprint of the firmware-only bbl smaller. The dummy payload message is updated to indicate the alternative load method.

This load method could also be supported by a first stage boot loader that reads seperate firmware and kernel from SPI flash. The main advantage of this new mechanism is that it eases kernel development by avoiding the riscv-pk packaging step after kernel builds, makes building per repository artefacts for CI simpler, and mimics bootloaders on other platforms that can load a kernel image file directly. Ultimately BBL should use an SPI driver to load the kernel image however this mechanism supports use cases such such as QEMU's -bios, -kernel and -initrd options following examples from other platforms that pass kernel entry to firmware via device-tree.

---
 hw/riscv/boot.c | 78
++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 72 insertions(+), 6 deletions(-)
hw/riscv/boot.c +26 -5
index e51e7f9..43f328d 100644
@@ -32,16 +32,70 @@
         fprintf(stderr, "boot: %s: "fs, __func__, ##__VA_ARGS__); \
     }
 
-static uint64_t load_kernel(const char *kernel_filename)
+static uint64_t kernel_offset;
+
+static uint64_t kernel_translate(void *opaque, uint64_t addr)
 {
-    uint64_t kernel_entry, kernel_high;
+    /* mask kernel virtual address and offset by load address */
+    if (kernel_offset) {
+        return (addr & 0x7fffffff) + kernel_offset;
+    } else {
+        return addr;
+    }
+}
 
-    if (load_elf(kernel_filename, NULL, NULL,
-                 &kernel_entry, NULL, &kernel_high,
+static hwaddr riscv_load_firmware(const char *filename)
+{
+    uint64_t firmware_entry, firmware_start, firmware_end;
+
+    if (load_elf(filename, NULL, NULL,
+                 &firmware_entry, &firmware_start, &firmware_end,
                  0, EM_RISCV, 1, 0) < 0) {
-        error_report("could not load kernel '%s'", kernel_filename);
+        error_report("could not load firmware '%s'", filename);
         exit(1);
     }
+
+    /* align kernel load address to the megapage after the firmware
 */
+#if defined(TARGET_RISCV32)
+    kernel_offset = (firmware_end + 0x3fffff) & ~0x3fffff;
+#else
+    kernel_offset = (firmware_end + 0x1fffff) & ~0x1fffff;
+#endif
+
+    boot_debug("entry=0x" TARGET_FMT_plx " start=0x" TARGET_FMT_plx

" "
+ "end=0x" TARGET_FMT_plx " kernel_offset=0x"
TARGET_FMT_plx "\n",
+ firmware_entry, firmware_start, firmware_end,
kernel_offset);

hw/riscv/boot.c +16 -0
+
+    return firmware_entry;
+}
+
+static hwaddr riscv_load_kernel(const char *filename, void *fdt)
+{
+    uint64_t kernel_entry, kernel_start, kernel_end;
+
+    if (load_elf(filename, kernel_translate, NULL,
+                 &kernel_entry, &kernel_start, &kernel_end,
+                 0, EM_RISCV, 1, 0) < 0) {
+        error_report("could not load kernel '%s'", filename);
+        exit(1);
+    }
+
+    boot_debug("entry=0x" TARGET_FMT_plx " start=0x" TARGET_FMT_plx

" "
+ "end=0x" TARGET_FMT_plx "\n", kernel_entry,
kernel_start,

hw/riscv/boot.c line 91

Quoted 7 lines
hw/riscv/boot.c +7 -0
+               kernel_end);
+
+    /*
+     * pass kernel load address via device-tree to firmware
+     *
+     * BBL reads the kernel address from device-tree
+     */

I don't think we should have bbl specific code in QEMU. bbl is not a long term solution so it seems like a bad idea to handle it forever in QEMU.

Do you mind fixing the comments I made to patch 1 and 2 and then sending the series to the QEMU mailing list?

If you would still like this patch then you can keep it in and we can have the discussion on the list about it's merrits.

Thanks for the series, the this is very helpful to QEMU and RISC-V support!

Alistair

Quoted 37 lines
hw/riscv/boot.c +15 -1
+    if (fdt) {
+        qemu_fdt_setprop_cells(fdt, "/chosen", "riscv,kernel-end",
+                               kernel_end >> 32, kernel_end);
+        qemu_fdt_setprop_cells(fdt, "/chosen", "riscv,kernel-start",
+                               kernel_start >> 32, kernel_start);
+    }
+
     return kernel_entry;
 }
@@ -91,8 +145,20 @@ hwaddr
 riscv_load_firmware_kernel_initrd(MachineState *machine, void *fdt)
 {
     hwaddr firmware_entry = 0;
 
+    /* load firmware e.g. -bios bbl */
+    if (machine->firmware) {
+        firmware_entry = riscv_load_firmware(machine->firmware);
+    }
+
+    /* load combined bbl+kernel or separate kernel */
     if (machine->kernel_filename) {
-        firmware_entry = load_kernel(machine->kernel_filename);
+        if (machine->firmware) {
+            /* load separate bios and kernel e.g. -bios bbl -kernel

vmlinux */

hw/riscv/boot.c +3 -0
+            riscv_load_kernel(machine->kernel_filename, fdt);
+        } else {
+            /* load traditional combined bbl+kernel e.g. -kernel

bbl_vmlimux */
+ firmware_entry = riscv_load_kernel(machine-

Quoted 1 line

kernel_filename, NULL);

+ }

         if (machine->initrd_filename) {
             /* load separate initrd */
             riscv_load_initrd(machine->initrd_filename, machine-
Quoted 1 line

ram_size,

#8 lore raw
In reply to Stefan O'Rear
Quoted 5 lines · Stefan O'Rear

This was posted in May[0] by Michael Clark. I have split it up to address earlier review feedback, along with other minor cleanups.

[0]:
http://lists.nongnu.org/archive/html/qemu-devel/2018-05/msg05121.html

Hello Stefan,

Thanks for picking this up. I'll review your patches today.

In the future can you please ensure you CC the QEMU mailing list on any patches submitted: [email protected]

Alistair

Quoted 16 lines

Stefan O'Rear (4):
RISC-V: Move firmware loading logic to a separate file
RISC-V: refactor initrd loading
RISC-V: Honor entry point in loaded ELF firmware
RISC-V: Support separate firmware and kernel payload

 hw/riscv/Makefile.objs  |   1 +
 hw/riscv/boot.c         | 170
++++++++++++++++++++++++++++++++++++++++++++++++
 hw/riscv/virt.c         |  69 ++++----------------
 include/hw/riscv/boot.h |  24 +++++++
 4 files changed, 208 insertions(+), 56 deletions(-)
 create mode 100644 hw/riscv/boot.c
 create mode 100644 include/hw/riscv/boot.h