Skip to contentSkip to conversation

hw/riscv: consolidate kernel init in riscv_load_kernel()

PATCH Superseded Daniel Henrique Barboza 3 participants 5 messages Jan 12, 2023 – Jan 13, 2023

Subsystems (2)

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v6 cover hw/riscv: consolidate kernel init in riscv_load_kernel()

Hi,

These are the 2 remaining patches, patches 10 and 11, of:

"[PATCH v5 00/11] riscv: OpenSBI boot test and cleanups"

The first 9 patches are already available in riscv-to-apply.next.

The only change made was in patch 10 where we're now handling the case where load_elf_ram_sym is padding the resulting kernel_entry with 1s for 32 bits. Patch 11 is unchanged.

Changes from v5:
- former patches 1-9: already pushed to riscv-to-apply.next
- patch 10:
- added an 'is_32bit' flag in riscv_load_kernel(). Use it to eliminate the sign-extension from load_elf() if we're running a 32-bit guest.
v5 link: https://lists.gnu.org/archive/html/qemu-devel/2023-01/msg00051.html

Daniel Henrique Barboza (2):
hw/riscv/boot.c: consolidate all kernel init in riscv_load_kernel() hw/riscv/boot.c: make riscv_load_initrd() static

 hw/riscv/boot.c            | 98 ++++++++++++++++++++++++--------------
 hw/riscv/microchip_pfsoc.c | 12 +----
 hw/riscv/opentitan.c       |  3 +-
 hw/riscv/sifive_e.c        |  4 +-
 hw/riscv/sifive_u.c        | 13 ++---
 hw/riscv/spike.c           | 10 +---
 hw/riscv/virt.c            | 13 ++---
 include/hw/riscv/boot.h    |  3 +-
 8 files changed, 77 insertions(+), 79 deletions(-)
Signature · 1 line
2.39.0
#1 lore raw

PATCH v6 1/2 hw/riscv/boot.c: consolidate all kernel init in riscv_load_kernel()

The microchip_icicle_kit, sifive_u, spike and virt boards are now doing the same steps when '-kernel' is used:

- execute load_kernel()
- load init_rd()
- write kernel_cmdline

Let's fold everything inside riscv_load_kernel() to avoid code repetition. To not change the behavior of boards that aren't calling riscv_load_initrd(), add an 'load_initrd' flag to riscv_load_kernel() and allow these boards to opt out from initrd loading.

---
 hw/riscv/boot.c            | 30 +++++++++++++++++++++++++++---
 hw/riscv/microchip_pfsoc.c | 12 ++----------
 hw/riscv/opentitan.c       |  3 ++-
 hw/riscv/sifive_e.c        |  4 +++-
 hw/riscv/sifive_u.c        | 13 +++----------
 hw/riscv/spike.c           | 10 +---------
 hw/riscv/virt.c            | 13 +++----------
 include/hw/riscv/boot.h    |  2 ++
 8 files changed, 43 insertions(+), 44 deletions(-)
hw/riscv/boot.c +27 -3
index 2594276223..e8e8b8517c 100644
@@ -175,10 +175,12 @@ target_ulong riscv_load_firmware(const char *firmware_filename,
 
 target_ulong riscv_load_kernel(MachineState *machine,
                                target_ulong kernel_start_addr,
+                               bool load_initrd, bool is_32bits,
                                symbol_fn_t sym_cb)
 {
     const char *kernel_filename = machine->kernel_filename;
     uint64_t kernel_load_base, kernel_entry;
+    void *fdt = machine->fdt;
 
     g_assert(kernel_filename != NULL);
 
@@ -192,21 +194,43 @@ target_ulong riscv_load_kernel(MachineState *machine,
     if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL,
                          NULL, &kernel_load_base, NULL, NULL, 0,
                          EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
-        return kernel_load_base;
+        kernel_entry = kernel_load_base;
+        /*
+         * kernel_load_base is sign-extended for 32 bits and can
+         * be padded with '1's. Do an uint32_t cast to avoid the
+         * padding if we're running a 32 bit CPU.
+         */
+        if (is_32bits) {
+            kernel_entry = (uint32_t)kernel_load_base;
+        }
+        goto out;
     }
 
     if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL,
                        NULL, NULL, NULL) > 0) {
-        return kernel_entry;
+        goto out;
     }
 
     if (load_image_targphys_as(kernel_filename, kernel_start_addr,
                                current_machine->ram_size, NULL) > 0) {
-        return kernel_start_addr;
+        kernel_entry = kernel_start_addr;
+        goto out;
     }
 
     error_report("could not load kernel '%s'", kernel_filename);
     exit(1);
+
+out:
+    if (load_initrd && machine->initrd_filename) {
+        riscv_load_initrd(machine, kernel_entry);
+    }
+
+    if (fdt && machine->kernel_cmdline && *machine->kernel_cmdline) {
+        qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
+                                machine->kernel_cmdline);
+    }
+
+    return kernel_entry;
 }
 
 void riscv_load_initrd(MachineState *machine, uint64_t kernel_entry)
hw/riscv/microchip_pfsoc.c +2 -10
index 82ae5e7023..cb9e126827 100644
@@ -629,16 +629,8 @@ static void microchip_icicle_kit_machine_init(MachineState *machine)
         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
                                                          firmware_end_addr);
 
-        kernel_entry = riscv_load_kernel(machine, kernel_start_addr, NULL);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen",
-                                    "bootargs", machine->kernel_cmdline);
-        }
+        kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, false, NULL);
 
         /* Compute the fdt load address in dram */
         fdt_load_addr = riscv_load_fdt(memmap[MICROCHIP_PFSOC_DRAM_LO].base,
hw/riscv/opentitan.c +2 -1
index 64d5d435b9..05f2cfde32 100644
@@ -101,7 +101,8 @@ static void opentitan_board_init(MachineState *machine)
     }
 
     if (machine->kernel_filename) {
-        riscv_load_kernel(machine, memmap[IBEX_DEV_RAM].base, NULL);
+        riscv_load_kernel(machine, memmap[IBEX_DEV_RAM].base,
+                          false, true, NULL);
     }
 }
 
hw/riscv/sifive_e.c +3 -1
index 3e3f4b0088..5969ae8131 100644
@@ -114,7 +114,9 @@ static void sifive_e_machine_init(MachineState *machine)
                           memmap[SIFIVE_E_DEV_MROM].base, &address_space_memory);
 
     if (machine->kernel_filename) {
-        riscv_load_kernel(machine, memmap[SIFIVE_E_DEV_DTIM].base, NULL);
+        riscv_load_kernel(machine, memmap[SIFIVE_E_DEV_DTIM].base,
+                          false, riscv_is_32bit(&s->soc.cpus),
+                          NULL);
     }
 }
 
hw/riscv/sifive_u.c +3 -10
index bac394c959..44f5a2ba27 100644
@@ -598,16 +598,9 @@ static void sifive_u_machine_init(MachineState *machine)
         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
                                                          firmware_end_addr);
 
-        kernel_entry = riscv_load_kernel(machine, kernel_start_addr, NULL);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
-                                    machine->kernel_cmdline);
-        }
+        kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, riscv_is_32bit(&s->soc.u_cpus),
+                                         NULL);
     } else {
        /*
         * If dynamic firmware is used, it doesn't know where is the next mode
hw/riscv/spike.c +1 -9
index bff9475686..4766152429 100644
@@ -308,16 +308,8 @@ static void spike_board_init(MachineState *machine)
                                                          firmware_end_addr);
 
         kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, riscv_is_32bit(&s->soc[0]),
                                          htif_symbol_callback);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
-                                    machine->kernel_cmdline);
-        }
     } else {
        /*
         * If dynamic firmware is used, it doesn't know where is the next mode
hw/riscv/virt.c +3 -10
index c8e35f861e..91f6b02983 100644
@@ -1281,16 +1281,9 @@ static void virt_machine_done(Notifier *notifier, void *data)
         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc[0],
                                                          firmware_end_addr);
 
-        kernel_entry = riscv_load_kernel(machine, kernel_start_addr, NULL);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
-                                    machine->kernel_cmdline);
-        }
+        kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, riscv_is_32bit(&s->soc[0]),
+                                         NULL);
     } else {
        /*
         * If dynamic firmware is used, it doesn't know where is the next mode
include/hw/riscv/boot.h +2 -0
index f94653a09b..d34f61e280 100644
@@ -45,6 +45,8 @@ target_ulong riscv_load_firmware(const char *firmware_filename,
                                  symbol_fn_t sym_cb);
 target_ulong riscv_load_kernel(MachineState *machine,
                                target_ulong firmware_end_addr,
+                               bool load_initrd,
+                               bool is_32bits,
                                symbol_fn_t sym_cb);
 void riscv_load_initrd(MachineState *machine, uint64_t kernel_entry);
 uint64_t riscv_load_fdt(hwaddr dram_start, uint64_t dram_size, void *fdt);
Signature · 1 line
2.39.0
#4 lore raw

PATCH v6 2/2 hw/riscv/boot.c: make riscv_load_initrd() static

In reply to Daniel Henrique Barboza

The only remaining caller is riscv_load_kernel_and_initrd() which belongs to the same file.

---
 hw/riscv/boot.c         | 80 ++++++++++++++++++++---------------------
 include/hw/riscv/boot.h |  1 -
 2 files changed, 40 insertions(+), 41 deletions(-)
hw/riscv/boot.c +40 -40
index e8e8b8517c..9ec041f727 100644
@@ -173,6 +173,46 @@ target_ulong riscv_load_firmware(const char *firmware_filename,
     exit(1);
 }
 
+static void riscv_load_initrd(MachineState *machine, uint64_t kernel_entry)
+{
+    const char *filename = machine->initrd_filename;
+    uint64_t mem_size = machine->ram_size;
+    void *fdt = machine->fdt;
+    hwaddr start, end;
+    ssize_t size;
+
+    g_assert(filename != NULL);
+
+    /*
+     * We want to put the initrd far enough into RAM that when the
+     * kernel is uncompressed it will not clobber the initrd. However
+     * 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
+     * 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
+     * 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);
+        if (size == -1) {
+            error_report("could not load ramdisk '%s'", filename);
+            exit(1);
+        }
+    }
+
+    /* Some RISC-V machines (e.g. opentitan) don't have a fdt. */
+    if (fdt) {
+        end = start + size;
+        qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", start);
+        qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", end);
+    }
+}
+
 target_ulong riscv_load_kernel(MachineState *machine,
                                target_ulong kernel_start_addr,
                                bool load_initrd, bool is_32bits,
@@ -233,46 +273,6 @@ out:
     return kernel_entry;
 }
 
-void riscv_load_initrd(MachineState *machine, uint64_t kernel_entry)
-{
-    const char *filename = machine->initrd_filename;
-    uint64_t mem_size = machine->ram_size;
-    void *fdt = machine->fdt;
-    hwaddr start, end;
-    ssize_t size;
-
-    g_assert(filename != NULL);
-
-    /*
-     * We want to put the initrd far enough into RAM that when the
-     * kernel is uncompressed it will not clobber the initrd. However
-     * 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
-     * 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
-     * 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);
-        if (size == -1) {
-            error_report("could not load ramdisk '%s'", filename);
-            exit(1);
-        }
-    }
-
-    /* Some RISC-V machines (e.g. opentitan) don't have a fdt. */
-    if (fdt) {
-        end = start + size;
-        qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start", start);
-        qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end", end);
-    }
-}
-
 uint64_t riscv_load_fdt(hwaddr dram_base, uint64_t mem_size, void *fdt)
 {
     uint64_t temp, fdt_addr;
include/hw/riscv/boot.h +0 -1
index d34f61e280..85c58c49c8 100644
@@ -48,7 +48,6 @@ target_ulong riscv_load_kernel(MachineState *machine,
                                bool load_initrd,
                                bool is_32bits,
                                symbol_fn_t sym_cb);
-void riscv_load_initrd(MachineState *machine, uint64_t kernel_entry);
 uint64_t riscv_load_fdt(hwaddr dram_start, uint64_t dram_size, void *fdt);
 void riscv_setup_rom_reset_vec(MachineState *machine, RISCVHartArrayState *harts,
                                hwaddr saddr,
Signature · 1 line
2.39.0
#2 lore raw

Re: [PATCH v6 1/2] hw/riscv/boot.c: consolidate all kernel init in riscv_load_kernel()

In reply to Daniel Henrique Barboza
Quoted 44 lines · Daniel Henrique Barboza

The microchip_icicle_kit, sifive_u, spike and virt boards are now doing the same steps when '-kernel' is used:

- execute load_kernel()
- load init_rd()
- write kernel_cmdline

Let's fold everything inside riscv_load_kernel() to avoid code repetition. To not change the behavior of boards that aren't calling riscv_load_initrd(), add an 'load_initrd' flag to riscv_load_kernel() and allow these boards to opt out from initrd loading.

---
 hw/riscv/boot.c            | 30 +++++++++++++++++++++++++++---
 hw/riscv/microchip_pfsoc.c | 12 ++----------
 hw/riscv/opentitan.c       |  3 ++-
 hw/riscv/sifive_e.c        |  4 +++-
 hw/riscv/sifive_u.c        | 13 +++----------
 hw/riscv/spike.c           | 10 +---------
 hw/riscv/virt.c            | 13 +++----------
 include/hw/riscv/boot.h    |  2 ++
 8 files changed, 43 insertions(+), 44 deletions(-)
hw/riscv/boot.c +2 -0
index 2594276223..e8e8b8517c 100644
@@ -175,10 +175,12 @@ target_ulong riscv_load_firmware(const char *firmware_filename,
 
 target_ulong riscv_load_kernel(MachineState *machine,
                                target_ulong kernel_start_addr,
+                               bool load_initrd, bool is_32bits,
                                symbol_fn_t sym_cb)
 {
     const char *kernel_filename = machine->kernel_filename;
     uint64_t kernel_load_base, kernel_entry;
+    void *fdt = machine->fdt;
 
     g_assert(kernel_filename != NULL);
 

hw/riscv/boot.c target_ulong riscv_load_kernel(MachineState *machine, · line 202

Quoted 10 lines
hw/riscv/boot.c +6 -1
@@ -192,21 +194,43 @@ target_ulong riscv_load_kernel(MachineState *machine,
     if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL,
                          NULL, &kernel_load_base, NULL, NULL, 0,
                          EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
-        return kernel_load_base;
+        kernel_entry = kernel_load_base;
+        /*
+         * kernel_load_base is sign-extended for 32 bits and can
+         * be padded with '1's. Do an uint32_t cast to avoid the
+         * padding if we're running a 32 bit CPU.
+         */

I see both kernel_load_base and kernel_entry are declared as a uint64_t, and load_elf_ram_sym() accepts a uint64_t parameter. Where does the sign-extension happen?

Quoted 163 lines
hw/riscv/boot.c +19 -2
+        if (is_32bits) {
+            kernel_entry = (uint32_t)kernel_load_base;
+        }
+        goto out;
     }
 
     if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL,
                        NULL, NULL, NULL) > 0) {
-        return kernel_entry;
+        goto out;
     }
 
     if (load_image_targphys_as(kernel_filename, kernel_start_addr,
                                current_machine->ram_size, NULL) > 0) {
-        return kernel_start_addr;
+        kernel_entry = kernel_start_addr;
+        goto out;
     }
 
     error_report("could not load kernel '%s'", kernel_filename);
     exit(1);
+
+out:
+    if (load_initrd && machine->initrd_filename) {
+        riscv_load_initrd(machine, kernel_entry);
+    }
+
+    if (fdt && machine->kernel_cmdline && *machine->kernel_cmdline) {
+        qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
+                                machine->kernel_cmdline);
+    }
+
+    return kernel_entry;
 }
 
 void riscv_load_initrd(MachineState *machine, uint64_t kernel_entry)
hw/riscv/microchip_pfsoc.c +2 -10
index 82ae5e7023..cb9e126827 100644
@@ -629,16 +629,8 @@ static void microchip_icicle_kit_machine_init(MachineState *machine)
         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
                                                          firmware_end_addr);
 
-        kernel_entry = riscv_load_kernel(machine, kernel_start_addr, NULL);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen",
-                                    "bootargs", machine->kernel_cmdline);
-        }
+        kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, false, NULL);
 
         /* Compute the fdt load address in dram */
         fdt_load_addr = riscv_load_fdt(memmap[MICROCHIP_PFSOC_DRAM_LO].base,
hw/riscv/opentitan.c +2 -1
index 64d5d435b9..05f2cfde32 100644
@@ -101,7 +101,8 @@ static void opentitan_board_init(MachineState *machine)
     }
 
     if (machine->kernel_filename) {
-        riscv_load_kernel(machine, memmap[IBEX_DEV_RAM].base, NULL);
+        riscv_load_kernel(machine, memmap[IBEX_DEV_RAM].base,
+                          false, true, NULL);
     }
 }
 
hw/riscv/sifive_e.c +3 -1
index 3e3f4b0088..5969ae8131 100644
@@ -114,7 +114,9 @@ static void sifive_e_machine_init(MachineState *machine)
                           memmap[SIFIVE_E_DEV_MROM].base, &address_space_memory);
 
     if (machine->kernel_filename) {
-        riscv_load_kernel(machine, memmap[SIFIVE_E_DEV_DTIM].base, NULL);
+        riscv_load_kernel(machine, memmap[SIFIVE_E_DEV_DTIM].base,
+                          false, riscv_is_32bit(&s->soc.cpus),
+                          NULL);
     }
 }
 
hw/riscv/sifive_u.c +3 -10
index bac394c959..44f5a2ba27 100644
@@ -598,16 +598,9 @@ static void sifive_u_machine_init(MachineState *machine)
         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus,
                                                          firmware_end_addr);
 
-        kernel_entry = riscv_load_kernel(machine, kernel_start_addr, NULL);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
-                                    machine->kernel_cmdline);
-        }
+        kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, riscv_is_32bit(&s->soc.u_cpus),
+                                         NULL);
     } else {
        /*
         * If dynamic firmware is used, it doesn't know where is the next mode
hw/riscv/spike.c +1 -9
index bff9475686..4766152429 100644
@@ -308,16 +308,8 @@ static void spike_board_init(MachineState *machine)
                                                          firmware_end_addr);
 
         kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, riscv_is_32bit(&s->soc[0]),
                                          htif_symbol_callback);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
-                                    machine->kernel_cmdline);
-        }
     } else {
        /*
         * If dynamic firmware is used, it doesn't know where is the next mode
hw/riscv/virt.c +3 -10
index c8e35f861e..91f6b02983 100644
@@ -1281,16 +1281,9 @@ static void virt_machine_done(Notifier *notifier, void *data)
         kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc[0],
                                                          firmware_end_addr);
 
-        kernel_entry = riscv_load_kernel(machine, kernel_start_addr, NULL);
-
-        if (machine->initrd_filename) {
-            riscv_load_initrd(machine, kernel_entry);
-        }
-
-        if (machine->kernel_cmdline && *machine->kernel_cmdline) {
-            qemu_fdt_setprop_string(machine->fdt, "/chosen", "bootargs",
-                                    machine->kernel_cmdline);
-        }
+        kernel_entry = riscv_load_kernel(machine, kernel_start_addr,
+                                         true, riscv_is_32bit(&s->soc[0]),
+                                         NULL);
     } else {
        /*
         * If dynamic firmware is used, it doesn't know where is the next mode
include/hw/riscv/boot.h +0 -0
index f94653a09b..d34f61e280 100644

include/hw/riscv/boot.h target_ulong riscv_load_firmware(const char *firmware_filename,

Quoted 9 lines
include/hw/riscv/boot.h +2 -1
@@ -45,6 +45,8 @@ target_ulong riscv_load_firmware(const char *firmware_filename,
                                  symbol_fn_t sym_cb);
 target_ulong riscv_load_kernel(MachineState *machine,
                                target_ulong firmware_end_addr,
+                               bool load_initrd,
+                               bool is_32bits,
                                symbol_fn_t sym_cb);
 void riscv_load_initrd(MachineState *machine, uint64_t kernel_entry);
 uint64_t riscv_load_fdt(hwaddr dram_start, uint64_t dram_size, void *fdt);
--

Regards,
Bin

#3 lore raw

Re: [PATCH v6 1/2] hw/riscv/boot.c: consolidate all kernel init in riscv_load_kernel()

Quoted 45 lines · Bin Meng
Quoted 43 lines · Daniel Henrique Barboza

The microchip_icicle_kit, sifive_u, spike and virt boards are now doing the same steps when '-kernel' is used:

- execute load_kernel()
- load init_rd()
- write kernel_cmdline

Let's fold everything inside riscv_load_kernel() to avoid code repetition. To not change the behavior of boards that aren't calling riscv_load_initrd(), add an 'load_initrd' flag to riscv_load_kernel() and allow these boards to opt out from initrd loading.

---
  hw/riscv/boot.c            | 30 +++++++++++++++++++++++++++---
  hw/riscv/microchip_pfsoc.c | 12 ++----------
  hw/riscv/opentitan.c       |  3 ++-
  hw/riscv/sifive_e.c        |  4 +++-
  hw/riscv/sifive_u.c        | 13 +++----------
  hw/riscv/spike.c           | 10 +---------
  hw/riscv/virt.c            | 13 +++----------
  include/hw/riscv/boot.h    |  2 ++
  8 files changed, 43 insertions(+), 44 deletions(-)
hw/riscv/boot.c +2 -0
index 2594276223..e8e8b8517c 100644
@@ -175,10 +175,12 @@ target_ulong riscv_load_firmware(const char *firmware_filename,
 
  target_ulong riscv_load_kernel(MachineState *machine,
                                 target_ulong kernel_start_addr,
+                               bool load_initrd, bool is_32bits,
                                 symbol_fn_t sym_cb)
  {
      const char *kernel_filename = machine->kernel_filename;
      uint64_t kernel_load_base, kernel_entry;
+    void *fdt = machine->fdt;
 
      g_assert(kernel_filename != NULL);
 
Quoted 15 lines

hw/riscv/boot.c target_ulong riscv_load_kernel(MachineState *machine, · line 202

Quoted 11 lines
hw/riscv/boot.c +6 -1
@@ -192,21 +194,43 @@ target_ulong riscv_load_kernel(MachineState *machine,
      if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL,
                           NULL, &kernel_load_base, NULL, NULL, 0,
                           EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
-        return kernel_load_base;
+        kernel_entry = kernel_load_base;
+        /*
+         * kernel_load_base is sign-extended for 32 bits and can
+         * be padded with '1's. Do an uint32_t cast to avoid the
+         * padding if we're running a 32 bit CPU.
+         */

I see both kernel_load_base and kernel_entry are declared as a uint64_t, and load_elf_ram_sym() accepts a uint64_t parameter. Where does the sign-extension happen?

Likely load_elf_ram_sym()'s translate_fn() argument is missing?

* @translate_fn: optional function to translate load addresses
* @translate_opaque: opaque data passed to @translate_fn

Others archs provide:

$ git grep -F '(void *opaque, uint64_t'
hw/alpha/dp264.c:23:static uint64_t cpu_alpha_superpage_to_phys(void *opaque, uint64_t addr)
hw/cris/boot.c:62:static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/hppa/machine.c:107:static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
hw/intc/openpic.c:857:static uint64_t openpic_src_read(void *opaque, uint64_t addr, unsigned len)
hw/loongarch/virt.c:390:static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
hw/microblaze/boot.c:112:static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/nios2/boot.c:78:static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/ppc/mac_newworld.c:117:static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/ppc/mac_oldworld.c:75:static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/ppc/spapr.c:1263:static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/s390x/ipl.c:106:static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr)
hw/sparc/sun4m.c:217:static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
hw/sparc/sun4m.c:673:static uint64_t translate_prom_address(void *opaque, uint64_t addr)
hw/sparc64/sun4u.c:412:static uint64_t translate_prom_address(void *opaque, uint64_t addr)
hw/ssi/xlnx-versal-ospi.c:1613:static void ospi_indac_write(void *opaque, uint64_t value, unsigned int size)
hw/timer/pxa2xx_timer.c:117:static void pxa2xx_timer_update(void *opaque, uint64_t now_qemu)
hw/timer/pxa2xx_timer.c:134:static void pxa2xx_timer_update4(void *opaque, uint64_t now_qemu, int n)
hw/xtensa/sim.c:40:static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
hw/xtensa/xtfpga.c:189:static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
Quoted 5 lines
Quoted 5 lines
hw/riscv/boot.c +4 -0
+        if (is_32bits) {
+            kernel_entry = (uint32_t)kernel_load_base;
+        }
+        goto out;
      }