Skip to contentSkip to conversation

arm: add device tree support

PATCH In review Grant Likely 6 participants 19 messages Feb 1, 2012 – Feb 22, 2012 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm: add device tree support

If compiled with CONFIG_FDT, allow user to specify a device tree file using the -dtb argument. If the machine supports it then the dtb will be loaded into memory and passed to the kernel on boot.

v2: - Enable for all arm platforms by making arm_boot use the filename directly
- Fix style issues

Cc Paul Brook <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Rob Herring <[email protected]>
Cc Edgar E. Iglesias <[email protected]>
---
 Makefile.target |    1 +
 configure       |    1 +
 hw/arm_boot.c   |  100 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 hw/boards.h     |    1 +
 qemu-options.hx |    9 +++++
 vl.c            |    9 +++++
 6 files changed, 113 insertions(+), 8 deletions(-)
Makefile.target +1 -0
index 68481a3..5e465ec 100644
@@ -363,6 +363,7 @@ obj-arm-y += vexpress.o
 obj-arm-y += strongarm.o
 obj-arm-y += collie.o
 obj-arm-y += pl041.o lm4549.o
+obj-arm-$(CONFIG_FDT) += device_tree.o
 
 obj-sh4-y = shix.o r2d.o sh7750.o sh7750_regnames.o tc58128.o
 obj-sh4-y += sh_timer.o sh_serial.o sh_intc.o sh_pci.o sm501.o
configure +1 -0
index f69e08f..2856897 100755
@@ -3411,6 +3411,7 @@ case "$target_arch2" in
     gdb_xml_files="arm-core.xml arm-vfp.xml arm-vfp3.xml arm-neon.xml"
     target_phys_bits=32
     target_llong_alignment=4
+    target_libs_softmmu="$fdt_libs"
   ;;
   cris)
     target_nptl="yes"
hw/arm_boot.c +92 -8

143 lines not on this page · raw message

hw/boards.h +1 -0
index f6d3784..d06776c 100644
@@ -34,5 +34,6 @@ typedef struct QEMUMachine {
 int qemu_register_machine(QEMUMachine *m);
 
 extern QEMUMachine *current_machine;
+extern const char *current_dtb_filename;
 
 #endif
qemu-options.hx +9 -0
index 3a07ae8..309a403 100644
@@ -1964,6 +1964,15 @@ Use @var{file1} and @var{file2} as modules and pass arg=foo as parameter to the
 first module.
 ETEXI
 
+DEF("dtb", HAS_ARG, QEMU_OPTION_dtb, \
+    "-dtb    file    use 'file' as device tree image\n", QEMU_ARCH_ARM)
+STEXI
+@item -dtb @var{file}
+@findex -dtb
+Use @var{file} as a device tree binary (dtb) image and pass it to the kernel
+on boot.
+ETEXI
+
 STEXI
 @end table
 ETEXI
vl.c +9 -0
index d88a18c..b22eae3 100644
@@ -1154,6 +1154,7 @@ void pcmcia_info(Monitor *mon)
 
 static QEMUMachine *first_machine = NULL;
 QEMUMachine *current_machine = NULL;
+const char *current_dtb_filename = NULL;
 
 int qemu_register_machine(QEMUMachine *m)
 {
@@ -2304,6 +2305,9 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_initrd:
                 initrd_filename = optarg;
                 break;
+            case QEMU_OPTION_dtb:
+                current_dtb_filename = optarg;
+                break;
             case QEMU_OPTION_hda:
                 {
                     char buf[256];
@@ -3241,6 +3245,11 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
+    if (!linux_boot && current_dtb_filename != NULL) {
+        fprintf(stderr, "-dtb only allowed with -kernel option\n");
+        exit(1);
+    }
+
     os_set_line_buffering();
 
     if (init_timer_alarm() < 0) {
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 192 lines · Grant Likely

If compiled with CONFIG_FDT, allow user to specify a device tree file using the -dtb argument. If the machine supports it then the dtb will be loaded into memory and passed to the kernel on boot.

v2: - Enable for all arm platforms by making arm_boot use the filename directly
- Fix style issues

Cc Paul Brook <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Rob Herring <[email protected]>
Cc Edgar E. Iglesias <[email protected]>

---

Makefile.target |    1 +
configure       |    1 +
hw/arm_boot.c   |  100 ++++++++++++++++++++++++++++++++++++++++++++++++++----
hw/boards.h     |    1 +
qemu-options.hx |    9 +++++
vl.c            |    9 +++++
6 files changed, 113 insertions(+), 8 deletions(-)
Makefile.target +0 -0
index 68481a3..5e465ec 100644
@@ -363,6 +363,7 @@ obj-arm-y += vexpress.o

obj-arm-y += strongarm.o
obj-arm-y += collie.o
obj-arm-y += pl041.o lm4549.o
+obj-arm-$(CONFIG_FDT) += device_tree.o

obj-sh4-y = shix.o r2d.o sh7750.o sh7750_regnames.o tc58128.o obj-sh4-y += sh_timer.o sh_serial.o sh_intc.o sh_pci.o sm501.o

configure +1 -0
index f69e08f..2856897 100755
@@ -3411,6 +3411,7 @@ case "$target_arch2" in
    gdb_xml_files="arm-core.xml arm-vfp.xml arm-vfp3.xml arm-neon.xml"
    target_phys_bits=32
    target_llong_alignment=4
+    target_libs_softmmu="$fdt_libs"
  ;;
  cris)
    target_nptl="yes"
hw/arm_boot.c +92 -7
index 5f163fd..377d202 100644
@@ -7,11 +7,14 @@
 * This code is licensed under the GPL.
 */
 
+#include "config.h"
 #include "hw.h"
 #include "arm-misc.h"
 #include "sysemu.h"
+#include "boards.h"
 #include "loader.h"
 #include "elf.h"
+#include "device_tree.h"
 
 #define KERNEL_ARGS_ADDR 0x100
 #define KERNEL_LOAD_ADDR 0x00010000
@@ -207,6 +210,71 @@ static void set_kernel_args_old(const struct arm_boot_info *info,
    }
 }
 
+static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
+{
+#ifdef CONFIG_FDT
+    uint32_t mem_reg_property[] = { cpu_to_be32(binfo->loader_start),
+                                    cpu_to_be32(binfo->ram_size) };
+    void *fdt = NULL;
+    char *filename;
+    int size, rc;
+
+    if (!current_dtb_filename) {
+        return 0;
+    }
+
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, current_dtb_filename);
+    if (!filename) {
+        fprintf(stderr, "Couldn't open dtb file %s\n", current_dtb_filename);
+        return -1;
+    }
+
+    fdt = load_device_tree(filename, &size);
+    if (!fdt) {
+        fprintf(stderr, "Couldn't open dtb file %s\n", filename);
+        g_free(filename);
+        return -1;
+    }
+    g_free(filename);
+
+    rc = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
+                               sizeof(mem_reg_property));
+    if (rc < 0) {
+        fprintf(stderr, "couldn't set /memory/reg\n");
+    }
+
+    rc = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
+                                      binfo->kernel_cmdline);
+    if (rc < 0) {
+        fprintf(stderr, "couldn't set /chosen/bootargs\n");
+    }
+
+    if (binfo->initrd_size) {
+        rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
+                binfo->loader_start + INITRD_LOAD_ADDR);
+        if (rc < 0) {
+            fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
+        }
+
+        rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
+                    binfo->loader_start + INITRD_LOAD_ADDR +
+                    binfo->initrd_size);
+        if (rc < 0) {
+            fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
+        }
+    }
+
+    cpu_physical_memory_write(addr, fdt, size);
+
+    return 0;
+
+#else
+    fprintf(stderr, "Platform requested a device tree, "
+                "but qemu was compiled without fdt support\n");
+    return -1;
+#endif
+}
+
 static void do_cpu_reset(void *opaque)
 {
    CPUState *env = opaque;
@@ -221,12 +289,14 @@ static void do_cpu_reset(void *opaque)
        } else {
            if (env == first_cpu) {
                env->regs[15] = info->loader_start;
-                if (old_param) {
-                    set_kernel_args_old(info, info->initrd_size,
+                if (!current_dtb_filename) {
+                    if (old_param) {
+                        set_kernel_args_old(info, info->initrd_size,
+                                            info->loader_start);
+                    } else {
+                        set_kernel_args(info, info->initrd_size,
                                        info->loader_start);
-                } else {
-                    set_kernel_args(info, info->initrd_size,
-                                    info->loader_start);
+                    }
                }
            } else {
                info->secondary_cpu_reset_hook(env, info);
@@ -242,7 +312,7 @@ void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
    int n;
    int is_linux = 0;
    uint64_t elf_entry;
-    target_phys_addr_t entry;
+    target_phys_addr_t entry, dtb_start;
    int big_endian;
 
    /* Load the kernel.  */
@@ -301,8 +371,23 @@ void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
        } else {
            initrd_size = 0;
        }
+        info->initrd_size = initrd_size;
+
+        /* Place the DTB after the initrd in memory */
+        dtb_start = TARGET_PAGE_ALIGN(info->loader_start + INITRD_LOAD_ADDR +
+                                      initrd_size);
+        if (load_dtb(dtb_start, info)) {
+            exit(1);
+        }
+
        bootloader[4] = info->board_id;
-        bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
+        /* for device tree boot, we pass the DTB directly in r2. Otherwise
+         * we point to the kernel args */
+        if (current_dtb_filename) {
+            bootloader[5] = dtb_start;
+        } else {
+            bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
+        }
        bootloader[6] = entry;
        for (n = 0; n < sizeof(bootloader) / 4; n++) {
            bootloader[n] = tswap32(bootloader[n]);

hw/arm_boot.c void arm_load_kernel(CPUState *env, struct arm_boot_info *info) · line 315

Quoted 4 lines
hw/arm_boot.c +0 -1
@@ -312,7 +397,6 @@ void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
        if (info->nb_cpus > 1) {
            info->write_secondary_boot(env, info);
        }
-        info->initrd_size = initrd_size;

This seems to be an unrelated change. Any reason for moving that line up?

hw/boards.h typedef struct QEMUMachine { · line 38

Quoted 13 lines
hw/arm_boot.c +0 -0
    }
    info->is_linux = is_linux;
 
hw/boards.h +1 -0
index f6d3784..d06776c 100644
@@ -34,5 +34,6 @@ typedef struct QEMUMachine {
 int qemu_register_machine(QEMUMachine *m);
 
 extern QEMUMachine *current_machine;
+extern const char *current_dtb_filename;
 

I'm not sure I like that naming. It really should only contain the cmdline option. If I have a default dtb file for my machine, I'd like to keep this variable empty.

Also, are you sure that boards.h is the right place for the extern declaration? In fact, is the global a good idea in the first place?

We could also just change machine->init() and pass the dtb in there. In a QOM world these would become machine device properties anyways.

    machine->init(ram_size, boot_devices,
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);

Essentially we shouldn't treat -dtb any different than -kernel or -initrd. It's also useful for more than ARM, namely embedded ppc systems. But I can easily post a follow-up patch for those.

Alex

#2 lore raw
Quoted 10 lines

We could also just change machine->init() and pass the dtb in there. In a QOM world these would become machine device properties anyways.

machine->init(ram_size, boot_devices,
kernel_filename, kernel_cmdline, initrd_filename,
cpu_model);

Essentially we shouldn't treat -dtb any different than -kernel or -initrd. It's also useful for more than ARM, namely embedded ppc systems. But I can easily post a follow-up patch for those.

Changing machine->init means you have to touch every single board file, and clone the exact same code for every machine that uses arm_boot.c. All of which will be rewritten in the near future.

machine->init is a particularly suckiy interface to start with, we want to be using it less, not more. It's not like we're going support multiple machine instanced. At least not before machine->init is removed altogether.

Paul

#3 lore raw
Quoted 7 lines · Paul Brook
Quoted 7 lines

We could also just change machine->init() and pass the dtb in there. In a QOM world these would become machine device properties anyways.

machine->init(ram_size, boot_devices,
kernel_filename, kernel_cmdline, initrd_filename,
cpu_model);

Quoted 7 lines
Quoted 3 lines

Essentially we shouldn't treat -dtb any different than -kernel or -initrd. It's also useful for more than ARM, namely embedded ppc systems. But I can easily post a follow-up patch for those.

Changing machine->init means you have to touch every single board file, and clone the exact same code for every machine that uses arm_boot.c. All of which will be rewritten in the near future.

Well, the dt file name would have to be passed into the generic arm_boot.c function, yes. But that's something that we need to do at one point in time either way, because machines will want to have default dtb file names.

Quoted 3 lines

machine->init is a particularly suckiy interface to start with, we want to be using it less, not more. It's not like we're going support multiple machine instanced. At least not before machine->init is removed altogether.

I do see your point on not extending legacy interfaces though and not bloating up the patch. In fact, I'm indifferent enough on the actual implementation atm, as long as the command line interface (or whatever the user sees) is reasonably sane. And it is IMHO. So if it makes everything easier, do it using a global, but keep in mind that this will need refactoring.

Alex

#4 lore raw
Quoted 18 lines · Alexander Graf
Quoted 7 lines · Paul Brook
Quoted 7 lines

We could also just change machine->init() and pass the dtb in there. In a QOM world these would become machine device properties anyways.

machine->init(ram_size, boot_devices,
kernel_filename, kernel_cmdline, initrd_filename,
cpu_model);

Quoted 7 lines
Quoted 3 lines

Essentially we shouldn't treat -dtb any different than -kernel or -initrd. It's also useful for more than ARM, namely embedded ppc systems. But I can easily post a follow-up patch for those.

Changing machine->init means you have to touch every single board file, and clone the exact same code for every machine that uses arm_boot.c. All of which will be rewritten in the near future.

Quoted 7 lines

Well, the dt file name would have to be passed into the generic arm_boot.c function, yes. But that's something that we need to do at one point in time either way, because machines will want to have default dtb file names.

Quoted 3 lines

machine->init is a particularly suckiy interface to start with, we want to be using it less, not more. It's not like we're going support multiple machine instanced. At least not before machine->init is removed altogether.

I do see your point on not extending legacy interfaces though and not bloating up the patch. In fact, I'm indifferent enough on the actual implementation atm, as long as the command line interface (or whatever the user sees) is reasonably sane. And it is IMHO. So if it makes everything easier, do it using a global, but keep in mind that this will need refactoring.

Globals are even worse!

Can't you hear the kernel loader begging to be turned into a device? It's pleading with us to stop abusing other parts of QEMU and make it a first class citizen of QEMU.

Regards,

Anthony Liguori

Quoted 5 lines

Alex

#5 lore raw
Quoted 45 lines · Anthony Liguori
Quoted 36 lines · Alexander Graf
Quoted 15 lines · Paul Brook
Quoted 8 lines

We could also just change machine->init() and pass the dtb in there. In a
QOM world these would become machine device properties anyways.

   machine->init(ram_size, boot_devices, kernel_filename, kernel_cmdline, initrd_filename, cpu_model);

Quoted 5 lines

Essentially we shouldn't treat -dtb any different than -kernel or -initrd.
It's also useful for more than ARM, namely embedded ppc systems. But I can
easily post a follow-up patch for those.

Quoted 4 lines

Changing machine->init means you have to touch every single board file, and
clone the exact same code for every machine that uses arm_boot.c.  All of which will be rewritten in the near future.

Well, the dt file name would have to be passed into the generic arm_boot.c function, yes. But that's something that we need to do at one point in time either way, because machines will want to have default dtb file names.

Quoted 5 lines

machine->init is a particularly suckiy interface to start with, we want to be
using it less, not more.  It's not like we're going support multiple machine
instanced.  At least not before machine->init is removed altogether.

Quoted 6 lines

I do see your point on not extending legacy interfaces though and not bloating up the patch. In fact, I'm indifferent enough on the actual implementation atm, as long as the command line interface (or whatever the user sees) is reasonably sane. And it is IMHO. So if it makes everything easier, do it using a global, but keep in mind that this will need refactoring.

Quoted 5 lines

Globals are even worse!

Can't you hear the kernel loader begging to be turned into a device?  It's pleading with us to stop abusing other parts of QEMU and make it a first class citizen of QEMU.

Is there some kind of initialisation phase where such a device can do its thing?

Unless I'm missing something a "loader" device will be racing the rest of the VM after reset to populate the memory with the desired contents, no?

John

Signature · 3 lines
John Williams, PhD, B. Eng, B. IT
PetaLogix - Linux Solutions for a Reconfigurable World
w: www.petalogix.com  p: +61-7-30090663  f: +61-7-30090663
#6 lore raw
Quoted 8 lines · John Williams
Quoted 6 lines · Anthony Liguori

Globals are even worse!

Can't you hear the kernel loader begging to be turned into a device? It's pleading with us to stop abusing other parts of QEMU and make it a first class citizen of QEMU.

Quoted 5 lines

Is there some kind of initialisation phase where such a device can do its thing?

Unless I'm missing something a "loader" device will be racing the rest of the VM after reset to populate the memory with the desired contents, no?

How does it race? Devices normally never touch memory so a loader device will be the only thing mucking with memory.

Regards,

Anthony Liguori

Quoted 1 line

John

#7 lore raw
Quoted 2 lines · Anthony Liguori

How does it race?  Devices normally never touch memory so a loader device will be the only thing mucking with memory.

The obvious one is "loader reset function wants to set starting PC to entry point of kernel/etc" vs "CPU device reset wants to set starting PC to hardware-mandated reset vector". We have this at the moment, of course, and I think we implicitly rely on reset handlers being called in order of registration...

(The other irritating case is where the CPU device reset wants to read the starting PC out of memory, like the Cortex-M3, but really that one is because we don't distinguish "going into reset" from "coming out of reset".)

-- PMM

#8 lore raw
Quoted 9 lines · Peter Maydell
Quoted 2 lines · Anthony Liguori

How does it race? Devices normally never touch memory so a loader device will be the only thing mucking with memory.

The obvious one is "loader reset function wants to set starting PC to entry point of kernel/etc" vs "CPU device reset wants to set starting PC to hardware-mandated reset vector". We have this at the moment, of course, and I think we implicitly rely on reset handlers being called in order of registration...

I'm a bit confused, why can't the kernel loader be implemented in terms of a firmware blob?

This is what we do for x86 and it solves this problem robustly. Isn't it just a matter of a few instructions to do a jmp to a known location?

Regards,

Anthony Liguori

Quoted 8 lines

(The other irritating case is where the CPU device reset wants to read the starting PC out of memory, like the Cortex-M3, but really that one is because we don't distinguish "going into reset" from "coming out of reset".)

-- PMM

#9 lore raw
Quoted 11 lines · Anthony Liguori
Quoted 9 lines · Peter Maydell
Quoted 2 lines · Anthony Liguori

How does it race? Devices normally never touch memory so a loader device will be the only thing mucking with memory.

The obvious one is "loader reset function wants to set starting PC to entry point of kernel/etc" vs "CPU device reset wants to set starting PC to hardware-mandated reset vector". We have this at the moment, of course, and I think we implicitly rely on reset handlers being called in order of registration...

Quoted 3 lines

I'm a bit confused, why can't the kernel loader be implemented in terms of a firmware blob?

This is what we do for x86 and it solves this problem robustly. Isn't it just a matter of a few instructions to do a jmp to a known location?

Only if you have non-semi-hosted modes. For e500 for example, we don't have a bios flash region mapped through mmio available. So we would have to write the "jump to kernel" code into ram. But where in RAM? Linux starts at address 0, so that one's taken.

Alex

#10 lore raw
Quoted 14 lines · Alexander Graf
Quoted 11 lines · Anthony Liguori
Quoted 9 lines · Peter Maydell
Quoted 2 lines · Anthony Liguori

How does it race? Devices normally never touch memory so a loader device will be the only thing mucking with memory.

The obvious one is "loader reset function wants to set starting PC to entry point of kernel/etc" vs "CPU device reset wants to set starting PC to hardware-mandated reset vector". We have this at the moment, of course, and I think we implicitly rely on reset handlers being called in order of registration...

Quoted 5 lines
Quoted 3 lines

I'm a bit confused, why can't the kernel loader be implemented in terms of a firmware blob?

This is what we do for x86 and it solves this problem robustly. Isn't it just a matter of a few instructions to do a jmp to a known location?

Only if you have non-semi-hosted modes. For e500 for example, we don't have a bios flash region mapped through mmio available. So we would have to write the "jump to kernel" code into ram. But where in RAM? Linux starts at address 0, so that one's taken.

The processor has to have a defined sequence where IP is fixed to a specific value, no?

How else would the real hardware bootstrap software?

Regards,

Anthony Liguori

Quoted 4 lines

Alex

#11 lore raw
Quoted 17 lines · Anthony Liguori
Quoted 16 lines · Alexander Graf
Quoted 13 lines · Anthony Liguori
Quoted 9 lines · Peter Maydell
Quoted 2 lines · Anthony Liguori

How does it race? Devices normally never touch memory so a loader device will be the only thing mucking with memory.

The obvious one is "loader reset function wants to set starting PC to entry point of kernel/etc" vs "CPU device reset wants to set starting PC to hardware-mandated reset vector". We have this at the moment, of course, and I think we implicitly rely on reset handlers being called in order of registration...

I'm a bit confused, why can't the kernel loader be implemented in terms of a firmware blob?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

This is what we do for x86 and it solves this problem robustly. Isn't it just a matter of a few instructions to do a jmp to a known location?

Only if you have non-semi-hosted modes. For e500 for example, we don't have a bios flash region mapped through mmio available. So we would have to write the "jump to kernel" code into ram. But where in RAM? Linux starts at address 0, so that one's taken.

The processor has to have a defined sequence where IP is fixed to a specific value, no?

How else would the real hardware bootstrap software?

Real hardware boots u-boot which initializes lots of things and then goes into the actual booting of Linux. Today, we're doing semi-hosting though, without u-boot. We just directly boot into Linux.

That's why I'm saying things don't work out all that simple with semi-hosted environments. Now you could argue that semi-hosting is a bad thing, but we'll always have to have it. On s390 for example, semi-hosting is how real hardware works. Or at least the parts that are visible to end users. Especially when you model PV machines, you'll have a hard time with fixed reset IPs too.

However, couldn't we model some wiring that allows our dash-kernel-boot-device to override the reset vector on CPUs?

Alex

#12 lore raw
Quoted 22 lines · Alexander Graf
Quoted 19 lines · Anthony Liguori
Quoted 18 lines · Alexander Graf
Quoted 14 lines · Anthony Liguori
Quoted 9 lines · Peter Maydell
Quoted 2 lines · Anthony Liguori

How does it race? Devices normally never touch memory so a loader device will be the only thing mucking with memory.

The obvious one is "loader reset function wants to set starting PC to entry point of kernel/etc" vs "CPU device reset wants to set starting PC to hardware-mandated reset vector". We have this at the moment, of course, and I think we implicitly rely on reset handlers being called in order of registration...

I'm a bit confused, why can't the kernel loader be implemented in terms of a firmware blob?

This is what we do for x86 and it solves this problem robustly. Isn't it just a matter of a few instructions to do a jmp to a known location?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Only if you have non-semi-hosted modes. For e500 for example, we don't have a bios flash region mapped through mmio available. So we would have to write the "jump to kernel" code into ram. But where in RAM? Linux starts at address 0, so that one's taken.

The processor has to have a defined sequence where IP is fixed to a specific value, no?

How else would the real hardware bootstrap software?

Real hardware boots u-boot which initializes lots of things and then goes into the actual booting of Linux. Today, we're doing semi-hosting though, without u-boot. We just directly boot into Linux.

Fine, but to boot u-boot, the real hardware must set IP to something that's most likely an offset into ROM flash.

Why can't we bootstrap semi-hosted mode by having a ROM somewhere that just redirects IP?

It doesn't have to be a full blown u-boot.

Quoted 2 lines

That's why I'm saying things don't work out all that simple with semi-hosted environments. Now you could argue that semi-hosting is a bad thing, but we'll always have to have it. On s390 for example, semi-hosting is how real hardware works. Or at least the parts that are visible to end users. Especially when you model PV machines, you'll have a hard time with fixed reset IPs too.

s390 is a special case because "real hardware" is not actually real hardware. It's a VM.

Regards,

Anthony Liguori

Quoted 6 lines

However, couldn't we model some wiring that allows our dash-kernel-boot-device to override the reset vector on CPUs?

Alex

#13 lore raw
Quoted 29 lines · Anthony Liguori
Quoted 28 lines · Alexander Graf
Quoted 24 lines · Anthony Liguori
Quoted 14 lines · Alexander Graf
Quoted 11 lines · Anthony Liguori
Quoted 9 lines · Peter Maydell
Quoted 2 lines · Anthony Liguori

How does it race? Devices normally never touch memory so a loader device will be the only thing mucking with memory.

The obvious one is "loader reset function wants to set starting PC to entry point of kernel/etc" vs "CPU device reset wants to set starting PC to hardware-mandated reset vector". We have this at the moment, of course, and I think we implicitly rely on reset handlers being called in order of registration...

Quoted 5 lines
Quoted 3 lines

I'm a bit confused, why can't the kernel loader be implemented in terms of a firmware blob?

This is what we do for x86 and it solves this problem robustly. Isn't it just a matter of a few instructions to do a jmp to a known location?

Only if you have non-semi-hosted modes. For e500 for example, we don't have a bios flash region mapped through mmio available. So we would have to write the "jump to kernel" code into ram. But where in RAM? Linux starts at address 0, so that one's taken.

The processor has to have a defined sequence where IP is fixed to a specific value, no?

How else would the real hardware bootstrap software?

Quoted 7 lines
Quoted 1 line

Real hardware boots u-boot which initializes lots of things and then goes into the actual booting of Linux. Today, we're doing semi-hosting though, without u-boot. We just directly boot into Linux.

Fine, but to boot u-boot, the real hardware must set IP to something that's most likely an offset into ROM flash.

Why can't we bootstrap semi-hosted mode by having a ROM somewhere that just redirects IP?

It doesn't have to be a full blown u-boot.

That would work, yes.

Quoted 5 lines
Quoted 3 lines

That's why I'm saying things don't work out all that simple with semi-hosted environments. Now you could argue that semi-hosting is a bad thing, but we'll always have to have it. On s390 for example, semi-hosting is how real hardware works. Or at least the parts that are visible to end users. Especially when you model PV machines, you'll have a hard time with fixed reset IPs too.

s390 is a special case because "real hardware" is not actually real hardware. It's a VM.

Sure, but how would we model things there? Our model needs to be flexible enough to cope with these oddballs.

In fact, s390 is even more complicated. For DASD boot, the CPU is stalled at first and instead the DASD controller reads some instructions from memory that then bootstrap the bootloader. But IIUC that's only the case for DASD boot. For zfcp boot, you basically get semi-hosting.

Alex

#14 lore raw
Quoted 12 lines · Alexander Graf
Quoted 5 lines · Anthony Liguori

Fine, but to boot u-boot, the real hardware must set IP to something that's most likely an offset into ROM flash.

Why can't we bootstrap semi-hosted mode by having a ROM somewhere that just redirects IP?

It doesn't have to be a full blown u-boot.

That would work, yes.

Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

That's why I'm saying things don't work out all that simple with semi-hosted environments. Now you could argue that semi-hosting is a bad thing, but we'll always have to have it. On s390 for example, semi-hosting is how real hardware works. Or at least the parts that are visible to end users. Especially when you model PV machines, you'll have a hard time with fixed reset IPs too.

s390 is a special case because "real hardware" is not actually real hardware. It's a VM.

Sure, but how would we model things there? Our model needs to be flexible enough to cope with these oddballs.

In fact, s390 is even more complicated. For DASD boot, the CPU is stalled at first and instead the DASD controller reads some instructions from memory that then bootstrap the bootloader. But IIUC that's only the case for DASD boot. For zfcp boot, you basically get semi-hosting.

Once CPUs are modeled QOM, my expectation is that we'll have something like a CPU::halted property. As part of realize, a CPU would set halted = true and that is what would trigger the CPU execution (be it through TCG or KVM).

There is no reason that on s390, the CPU realize function couldn't avoid setting halted=true and instead allow another device (with a wider view of the system) to perform some additional initialization work and then set the CPU halted property to true.

This is all about what causes the system to start running. Once we move to a property realize() model, it gives us a lot more flexibility to work through these types of dependency issues.

Regards,

Anthony Liguori

Quoted 4 lines

Alex

#15 lore raw
In reply to Alexander Graf
Quoted 18 lines · Alexander Graf
Quoted 7 lines · Paul Brook
Quoted 7 lines

We could also just change machine->init() and pass the dtb in there. In a QOM world these would become machine device properties anyways.

   machine->init(ram_size, boot_devices, kernel_filename, kernel_cmdline, initrd_filename, cpu_model);

Quoted 7 lines
Quoted 3 lines

Essentially we shouldn't treat -dtb any different than -kernel or -initrd. It's also useful for more than ARM, namely embedded ppc systems. But I can easily post a follow-up patch for those.

Changing machine->init means you have to touch every single board file, and clone the exact same code for every machine that uses arm_boot.c.  All of which will be rewritten in the near future.

Quoted 7 lines

Well, the dt file name would have to be passed into the generic arm_boot.c function, yes. But that's something that we need to do at one point in time either way, because machines will want to have default dtb file names.

Quoted 3 lines

machine->init is a particularly suckiy interface to start with, we want to be using it less, not more.  It's not like we're going support multiple machine instanced.  At least not before machine->init is removed altogether.

I do see your point on not extending legacy interfaces though and not bloating up the patch. In fact, I'm indifferent enough on the actual implementation atm, as long as the command line interface (or whatever the user sees) is reasonably sane. And it is IMHO. So if it makes everything easier, do it using a global, but keep in mind that this will need refactoring.

That's certainly my expectation. My initial instinct was also to handle it the say way as initrd and kernel pointers, but as Paul pointed out it requires touching all init functions which is a dead end effort when ->init() gets killed off. This patch is trivial to get the functionality into qemu without making it any more difficult for whoever creates the arm-kernel-loader device that Anthony is talking about.

g.

#16 lore raw
Quoted 27 lines · Grant Likely
Quoted 18 lines · Alexander Graf
Quoted 7 lines · Paul Brook
Quoted 7 lines

We could also just change machine->init() and pass the dtb in there. In a QOM world these would become machine device properties anyways.

machine->init(ram_size, boot_devices,
kernel_filename, kernel_cmdline, initrd_filename,
cpu_model);

Quoted 7 lines
Quoted 3 lines

Essentially we shouldn't treat -dtb any different than -kernel or -initrd. It's also useful for more than ARM, namely embedded ppc systems. But I can easily post a follow-up patch for those.

Changing machine->init means you have to touch every single board file, and clone the exact same code for every machine that uses arm_boot.c. All of which will be rewritten in the near future.

Quoted 7 lines

Well, the dt file name would have to be passed into the generic arm_boot.c function, yes. But that's something that we need to do at one point in time either way, because machines will want to have default dtb file names.

Quoted 3 lines

machine->init is a particularly suckiy interface to start with, we want to be using it less, not more. It's not like we're going support multiple machine instanced. At least not before machine->init is removed altogether.

I do see your point on not extending legacy interfaces though and not bloating up the patch. In fact, I'm indifferent enough on the actual implementation atm, as long as the command line interface (or whatever the user sees) is reasonably sane. And it is IMHO. So if it makes everything easier, do it using a global, but keep in mind that this will need refactoring.

Quoted 7 lines

That's certainly my expectation. My initial instinct was also to handle it the say way as initrd and kernel pointers, but as Paul pointed out it requires touching all init functions which is a dead end effort when ->init() gets killed off. This patch is trivial to get the functionality into qemu without making it any more difficult for whoever creates the arm-kernel-loader device that Anthony is talking about.

Yeah, I agree. Let's separate the QOM efforts from making things work for now. I don't want to have yet another if=ahci or hotplug magic where I'm waiting for a year for salvation that never came. Let's get the feature in and model the whole thing properly with all cases taken into account. This way we at least don't forget about dtbs when modeling the arm-kernel-loader device :)

Alex

#17 lore raw
In reply to Paul Brook
Quoted 11 lines · Paul Brook
Quoted 10 lines

We could also just change machine->init() and pass the dtb in there. In a QOM world these would become machine device properties anyways.

machine->init(ram_size, boot_devices,
kernel_filename, kernel_cmdline, initrd_filename,
cpu_model);

Essentially we shouldn't treat -dtb any different than -kernel or -initrd. It's also useful for more than ARM, namely embedded ppc systems. But I can easily post a follow-up patch for those.

Quoted 7 lines

Changing machine->init means you have to touch every single board file, and clone the exact same code for every machine that uses arm_boot.c. All of which will be rewritten in the near future.

machine->init is a particularly suckiy interface to start with, we want to be using it less, not more. It's not like we're going support multiple machine instanced. At least not before machine->init is removed altogether.

The right solution to this problem is to make an arm-kernel-loader device that has a dtb property.

It's a very small amount of code and fits with our longer term strategy of killing off machine->init.

Regards,

Anthony Liguori

Quoted 3 lines

Paul

#18 lore raw
In reply to Alexander Graf
Quoted 8 lines · Alexander Graf

hw/arm_boot.c void arm_load_kernel(CPUState *env, struct arm_boot_info *info) · line 315

Quoted 5 lines · Grant Likely
hw/arm_boot.c +0 -1
@@ -312,7 +397,6 @@ void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
         if (info->nb_cpus > 1) {
             info->write_secondary_boot(env, info);
         }
-        info->initrd_size = initrd_size;

This seems to be an unrelated change. Any reason for moving that line up?

I've just discovered the reason for this, which is that load_dtb() reads info->initrd_size, so without this change it all works fine except that we silently don't pass the initrd to the kernel :-)

-- PMM