Skip to contentSkip to conversation

OpenRISC Semihosting and Virt

RFC PATCH In review Stafford Horne 8 participants 29 messages May 27, 2022 – Dec 11, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover OpenRISC Semihosting and Virt

Hello,

I provide 2 options here 2 help with OpenRISC CI testing sush as the wireguard testing that Jason has been working on.

The two are:

1. Add semihosting to openrisc to handle l.nop based Halt and Reset
2. Define a new virt platform, this includes widing in the sifive test device that provides a syscon interface to allow for shutdown and reboot.

We could upstream both of them or just one. I am leaning toward dropping the semi-hosting work and just going ahead with virt.

The semi-hosting stuff was discussed here:

- https://www.mail-archive.com/[email protected]/msg884560.html

Also, I started to propose architecture changes here:

- https://github.com/openrisc/openrisc.github.io/pull/18

However, looking at how other platforms define semihosting it seems the openrisc requirements are quite different. We do not use the open/close/write etc syscalls as riscv, arm etc do.

Stafford Horne (3):
target/openrisc: Add basic support for semihosting hw/openrisc: Split re-usable boot time apis out to boot.c hw/openrisc: Add the OpenRISC virtual machine

 configs/devices/or1k-softmmu/default.mak |   3 +
 hw/openrisc/Kconfig                      |   9 +
 hw/openrisc/boot.c                       | 127 +++++++
 hw/openrisc/meson.build                  |   2 +
 hw/openrisc/openrisc_sim.c               | 106 +-----
 hw/openrisc/virt.c                       | 429 +++++++++++++++++++++++
 include/hw/openrisc/boot.h               |  34 ++
 qemu-options.hx                          |  16 +-
 target/openrisc/cpu.h                    |   2 +
 target/openrisc/helper.h                 |   1 +
 target/openrisc/meson.build              |   1 +
 target/openrisc/openrisc-semi.c          |  54 +++
 target/openrisc/sys_helper.c             |   5 +
 target/openrisc/translate.c              |   6 +
 14 files changed, 690 insertions(+), 105 deletions(-)
 create mode 100644 hw/openrisc/boot.c
 create mode 100644 hw/openrisc/virt.c
 create mode 100644 include/hw/openrisc/boot.h
 create mode 100644 target/openrisc/openrisc-semi.c
Signature · 1 line
2.31.1
#1 lore raw

RFC PATCH 1/3 target/openrisc: Add basic support for semihosting

For OpenRISC we currently only use semihosting for system exit and reset. This patch implements that.

The implementation uses a helper to delegate to the semihosting facility. The helper is marked as having side effects but currently does not have any. I have defined it like this as our other unimplemented semihosting calls will have side effects and return results in register r11.

---
 configs/devices/or1k-softmmu/default.mak |  2 +
 qemu-options.hx                          | 16 ++++---
 target/openrisc/cpu.h                    |  2 +
 target/openrisc/helper.h                 |  1 +
 target/openrisc/meson.build              |  1 +
 target/openrisc/openrisc-semi.c          | 54 ++++++++++++++++++++++++
 target/openrisc/sys_helper.c             |  5 +++
 target/openrisc/translate.c              |  6 +++
 8 files changed, 82 insertions(+), 5 deletions(-)
 create mode 100644 target/openrisc/openrisc-semi.c
configs/devices/or1k-softmmu/default.mak +2 -0
index 168101c39a..5b3ac89491 100644
@@ -1,5 +1,7 @@
 # Default configuration for or1k-softmmu
 
+CONFIG_SEMIHOSTING=y
+
 # Boards:
 #
 CONFIG_OR1K_SIM=y
qemu-options.hx +11 -5
index b484640067..312c68b065 100644
@@ -4566,10 +4566,12 @@ ERST
 DEF("semihosting", 0, QEMU_OPTION_semihosting,
     "-semihosting    semihosting mode\n",
     QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA |
-    QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
+    QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV |
+    QEMU_ARCH_OPENRISC)
 SRST
 ``-semihosting``
-    Enable semihosting mode (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V only).
+    Enable semihosting mode (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V,
+    OpenRISC only).
 
     Note that this allows guest direct access to the host filesystem, so
     should only be used with a trusted guest OS.
@@ -4581,11 +4583,12 @@ DEF("semihosting-config", HAS_ARG, QEMU_OPTION_semihosting_config,
     "-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,arg=str[,...]]\n" \
     "                semihosting configuration\n",
 QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA |
-QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV)
+QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2 | QEMU_ARCH_RISCV |
+QEMU_ARCH_OPENRISC)
 SRST
 ``-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,arg=str[,...]]``
-    Enable and configure semihosting (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V
-    only).
+    Enable and configure semihosting (ARM, M68K, Xtensa, MIPS, Nios II, RISC-V,
+    OpenRISC only).
 
     Note that this allows guest direct access to the host filesystem, so
     should only be used with a trusted guest OS.
@@ -4601,6 +4604,9 @@ SRST
 
     On RISC-V this implements the standard semihosting API, version 0.2.
 
+    On OpenRISC this only supports providing simulation exit and reset
+    facilities.
+
     ``target=native|gdb|auto``
         Defines where the semihosting calls will be addressed, to QEMU
         (``native``) or to GDB (``gdb``). The default is ``auto``, which
target/openrisc/cpu.h +2 -0
index b9584f10d4..4617f1272b 100644
@@ -407,4 +407,6 @@ void cpu_set_fpcsr(CPUOpenRISCState *env, uint32_t val);
 
 #define CPU_INTERRUPT_TIMER   CPU_INTERRUPT_TGT_INT_0
 
+void do_or1k_semihosting(CPUOpenRISCState *env, uint32_t k);
+
 #endif /* OPENRISC_CPU_H */
target/openrisc/helper.h +1 -0
index d847814a28..2fe3e4e4ca 100644
@@ -64,3 +64,4 @@ DEF_HELPER_FLAGS_1(rfe, 0, void, env)
 /* sys */
 DEF_HELPER_FLAGS_3(mtspr, 0, void, env, tl, tl)
 DEF_HELPER_FLAGS_3(mfspr, TCG_CALL_NO_WG, tl, env, tl, tl)
+DEF_HELPER_FLAGS_2(nop, 0, void, env, i32)
target/openrisc/meson.build +1 -0
index 84322086ec..1c1758b846 100644
@@ -10,6 +10,7 @@ openrisc_ss.add(files(
   'fpu_helper.c',
   'gdbstub.c',
   'interrupt_helper.c',
+  'openrisc-semi.c',
   'sys_helper.c',
   'translate.c',
 ))
target/openrisc/openrisc-semi.c new file +54 -0

54 lines not on this page · raw message

target/openrisc/sys_helper.c +5 -0
index 48674231e7..eb698a527e 100644
@@ -314,3 +314,8 @@ target_ulong HELPER(mfspr)(CPUOpenRISCState *env, target_ulong rd,
     /* for rd is passed in, if rd unchanged, just keep it back.  */
     return rd;
 }
+
+void HELPER(nop)(CPUOpenRISCState *env, target_ulong k)
+{
+    do_or1k_semihosting(env, k);
+}
target/openrisc/translate.c +6 -0
index 7b8ad43d5f..ec7b3b46ad 100644
@@ -28,6 +28,7 @@
 #include "qemu/qemu-print.h"
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
+#include "semihosting/semihost.h"
 
 #include "exec/helper-proto.h"
 #include "exec/helper-gen.h"
@@ -780,6 +781,11 @@ static bool trans_l_sh(DisasContext *dc, arg_store *a)
 
 static bool trans_l_nop(DisasContext *dc, arg_l_nop *a)
 {
+    if (semihosting_enabled() &&
+        a->k != 0) {
+        gen_helper_nop(cpu_env, tcg_constant_i32(a->k));
+    }
+
     return true;
 }
 
Signature · 1 line
2.31.1
#2 lore raw

Re: [RFC PATCH 1/3] target/openrisc: Add basic support for semihosting

Quoted 1 line · Stafford Horne

+void do_or1k_semihosting(CPUOpenRISCState *env, uint32_t k);

...

Quoted 1 line

+DEF_HELPER_FLAGS_2(nop, 0, void, env, i32)

Just call the helper "semihosting" and be done with it. And the helper wants an ifdef for system mode.

target/openrisc/meson.build openrisc_ss.add(files( · line 16

Quoted 8 lines
target/openrisc/meson.build +1 -0
@@ -10,6 +10,7 @@ openrisc_ss.add(files(
    'fpu_helper.c',
    'gdbstub.c',
    'interrupt_helper.c',
+  'openrisc-semi.c',
    'sys_helper.c',
    'translate.c',
  ))

You want to add the new file for system mode only.
Or, now that I think of it, conditional on CONFIG_SEMIHOSTING itself.

target/openrisc/openrisc-semi.c line 33

Quoted 4 lines
target/openrisc/openrisc-semi.c new file +4 -0
+static void or1k_semi_return_u32(CPUOpenRISCState *env, uint32_t ret)
+{
+    cpu_set_gpr(env, 11, ret);
+}

Let's drop this until you actually use it. This appears to be attempting to mirror other, more complete semihosting, but missing the third "error" argument.

target/openrisc/openrisc-semi.c line 47

Quoted 13 lines
target/openrisc/openrisc-semi.c new file +13 -0
+void do_or1k_semihosting(CPUOpenRISCState *env, uint32_t k)
+{
+    uint32_t result;
+
+    switch (k) {
+    case HOSTED_EXIT:
+        gdb_exit(cpu_get_gpr(env, 3));
+        exit(cpu_get_gpr(env, 3));
+    case HOSTED_RESET:
+#ifndef CONFIG_USER_ONLY
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
+        return;
+#endif

Do you in fact want to exit to the main loop after asking for reset? That's the only way that "no return value" makes sense to me...

target/openrisc/openrisc-semi.c line 50

Quoted 3 lines
target/openrisc/openrisc-semi.c new file +3 -0
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "or1k-semihosting: unsupported "
+                      "semihosting syscall %d\n", k);

%u.

target/openrisc/translate.c static bool trans_l_sh(DisasContext *dc, arg_store *a) · line 787

Quoted 6 lines
  static bool trans_l_nop(DisasContext *dc, arg_l_nop *a)
  {
target/openrisc/translate.c +4 -0
+    if (semihosting_enabled() &&
+        a->k != 0) {
+        gen_helper_nop(cpu_env, tcg_constant_i32(a->k));
+    }

Perhaps cleaner to move the semihosting dispatch switch here, instead of leaving it to the runtime? The reason we have a runtime switch for other guests is that the semihosting syscall number is in a register. This would then be

    if (semihosting_enabled()) {
        switch (a->k) {
        case 0:
            break; /* normal nop */
        case HOSTED_EXIT:
            gen_helper_semihost_exit(cpu_R(dc, 3));
            break;
        case HOSTED_RESET:
            gen_helper_semihost_reset();
            tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4);

            dc->base.is_jmp = DISAS_EXIT;
            break;
        ...
        }
    }

r~

#3 lore raw

Re: [RFC PATCH 1/3] target/openrisc: Add basic support for semihosting

Quoted 8 lines · Richard Henderson
Quoted 1 line · Stafford Horne

+void do_or1k_semihosting(CPUOpenRISCState *env, uint32_t k);

...

Quoted 1 line

+DEF_HELPER_FLAGS_2(nop, 0, void, env, i32)

Just call the helper "semihosting" and be done with it. And the helper wants an ifdef for system mode.

Quoted 11 lines

target/openrisc/meson.build openrisc_ss.add(files( · line 16

Quoted 8 lines
target/openrisc/meson.build +1 -0
@@ -10,6 +10,7 @@ openrisc_ss.add(files(
    'fpu_helper.c',
    'gdbstub.c',
    'interrupt_helper.c',
+  'openrisc-semi.c',
    'sys_helper.c',
    'translate.c',
  ))

You want to add the new file for system mode only.
Or, now that I think of it, conditional on CONFIG_SEMIHOSTING itself.

That's right, I'll update it.

Quoted 8 lines

target/openrisc/openrisc-semi.c line 33

Quoted 4 lines
target/openrisc/openrisc-semi.c new file +4 -0
+static void or1k_semi_return_u32(CPUOpenRISCState *env, uint32_t ret)
+{
+    cpu_set_gpr(env, 11, ret);
+}

Let's drop this until you actually use it. This appears to be attempting to mirror other, more complete semihosting, but missing the third "error" argument

Sure, I did mention I kept these here for future (real) semihosting support. But I don't think that will happen. So I can remove.

Quoted 8 lines
Quoted 8 lines
target/openrisc/openrisc-semi.c new file +8 -0
+void do_or1k_semihosting(CPUOpenRISCState *env, uint32_t k)
+{
+    uint32_t result;
+
+    switch (k) {
+    case HOSTED_EXIT:
+        gdb_exit(cpu_get_gpr(env, 3));
+        exit(cpu_get_gpr(env, 3));
Quoted 8 lines

target/openrisc/openrisc-semi.c line 47

Quoted 5 lines
target/openrisc/openrisc-semi.c new file +5 -0
+    case HOSTED_RESET:
+#ifndef CONFIG_USER_ONLY
+        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
+        return;
+#endif

Do you in fact want to exit to the main loop after asking for reset? That's the only way that "no return value" makes sense to me...

OK. I'll look at this more.

Quoted 5 lines

target/openrisc/openrisc-semi.c line 50

Quoted 3 lines
target/openrisc/openrisc-semi.c new file +3 -0
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "or1k-semihosting: unsupported "
+                      "semihosting syscall %d\n", k);

%u.

OK.

Quoted 12 lines

target/openrisc/translate.c static bool trans_l_sh(DisasContext *dc, arg_store *a) · line 787

Quoted 6 lines
  static bool trans_l_nop(DisasContext *dc, arg_l_nop *a)
  {
target/openrisc/translate.c +4 -0
+    if (semihosting_enabled() &&
+        a->k != 0) {
+        gen_helper_nop(cpu_env, tcg_constant_i32(a->k));
+    }

Perhaps cleaner to move the semihosting dispatch switch here, instead of leaving it to the runtime? The reason we have a runtime switch for other guests is that the semihosting syscall number is in a register. This would then be

Quoted 16 lines
    if (semihosting_enabled()) {
        switch (a->k) {
        case 0:
            break; /* normal nop */
        case HOSTED_EXIT:
            gen_helper_semihost_exit(cpu_R(dc, 3));
            break;
        case HOSTED_RESET:
            gen_helper_semihost_reset();
            tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4);

            dc->base.is_jmp = DISAS_EXIT;
            break;
        ...
        }
    }

Yeah, that makes sense. I had written it in a way that would allow expanding for real semi-hosting. But I don't think we will do that with OpenRISC, so this is good enough.

I am not sure if you saw the cover letter. I sent this RFC series to help illustrate two options for providing OpenRISC targets that support poweroff and reset.

One option being using these NOP's, the second is to create a virt target with reset/poweroff hardware.

I am kind of leaning towards dropping the semi-hosting patches and only moving forward with the virt patches. The reason being that 1. we would not need to expand the architecture spec to support the qemu virt platform, and we would need to document the NOP's formally, and 2. OpenRISC doesn't really support the full "semihosting" facilities for file open/close/write etc.

Any thoughts? I guess this "semihosting" patch is pretty trivial. But, maybe it causes more confusion compared to just going with the virt route. Also, if we have virt I can't imagine anyone using the semihosting much.

-Stafford

#4 lore raw

Re: [RFC PATCH 1/3] target/openrisc: Add basic support for semihosting

Quoted 5 lines · Stafford Horne

I am kind of leaning towards dropping the semi-hosting patches and only moving forward with the virt patches. The reason being that 1. we would not need to expand the architecture spec to support the qemu virt platform, and we would need to document the NOP's formally, and 2. OpenRISC doesn't really support the full "semihosting" facilities for file open/close/write etc.

I agree that "virt" would to more for openrisc devel than these nops.

Quoted 1 line

Also, if we have virt I can't imagine anyone using the semihosting much.

IMO, semihosting is most valuable for writing regression tests and not much more. (You have no control over the exit status of qemu with normal shutdown, as compared with semihosting exit.)

r~

#5 lore raw

RFC PATCH 2/3 hw/openrisc: Split re-usable boot time apis out to boot.c

In reply to Stafford Horne

These will be shared with the virt platform.

---
 hw/openrisc/boot.c         | 127 +++++++++++++++++++++++++++++++++++++
 hw/openrisc/meson.build    |   1 +
 hw/openrisc/openrisc_sim.c | 106 ++-----------------------------
 include/hw/openrisc/boot.h |  34 ++++++++++
 4 files changed, 168 insertions(+), 100 deletions(-)
 create mode 100644 hw/openrisc/boot.c
 create mode 100644 include/hw/openrisc/boot.h
hw/openrisc/boot.c new file +127 -0
new file mode 100644
index 0000000000..32f0925a8c
@@ -0,0 +1,127 @@
+/*
+ * QEMU OpenRISC boot helpers.
+ *
+ * Copyright (c) 2022 Stafford Horne <[email protected]>
+ *
+ * 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 "qemu/osdep.h"
+#include "cpu.h"
+#include "exec/cpu-defs.h"
+#include "elf.h"
+#include "hw/loader.h"
+#include "hw/openrisc/boot.h"
+#include "sysemu/device_tree.h"
+#include "sysemu/qtest.h"
+
+#include <libfdt.h>
+
+#define KERNEL_LOAD_ADDR 0x100
+
+hwaddr openrisc_load_kernel(ram_addr_t ram_size,
+                            const char *kernel_filename,
+                            uint32_t *bootstrap_pc)
+{
+    long kernel_size;
+    uint64_t elf_entry;
+    uint64_t high_addr;
+    hwaddr entry;
+
+    if (kernel_filename && !qtest_enabled()) {
+        kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
+                               &elf_entry, NULL, &high_addr, NULL, 1,
+                               EM_OPENRISC, 1, 0);
+        entry = elf_entry;
+        if (kernel_size < 0) {
+            kernel_size = load_uimage(kernel_filename,
+                                      &entry, NULL, NULL, NULL, NULL);
+            high_addr = entry + kernel_size;
+        }
+        if (kernel_size < 0) {
+            kernel_size = load_image_targphys(kernel_filename,
+                                              KERNEL_LOAD_ADDR,
+                                              ram_size - KERNEL_LOAD_ADDR);
+            high_addr = KERNEL_LOAD_ADDR + kernel_size;
+        }
+
+        if (entry <= 0) {
+            entry = KERNEL_LOAD_ADDR;
+        }
+
+        if (kernel_size < 0) {
+            error_report("couldn't load the kernel '%s'", kernel_filename);
+            exit(1);
+        }
+        *bootstrap_pc = entry;
+
+        return high_addr;
+    }
+    return 0;
+}
+
+hwaddr openrisc_load_initrd(void *fdt, const char *filename,
+                            hwaddr load_start, uint64_t mem_size)
+{
+    int size;
+    hwaddr start;
+
+    /* We put the initrd right after the kernel; page aligned. */
+    start = TARGET_PAGE_ALIGN(load_start);
+
+    size = load_ramdisk(filename, start, mem_size - start);
+    if (size < 0) {
+        size = load_image_targphys(filename, start, mem_size - start);
+        if (size < 0) {
+            error_report("could not load ramdisk '%s'", filename);
+            exit(1);
+        }
+    }
+
+    if (fdt) {
+        qemu_fdt_setprop_cell(fdt, "/chosen",
+                              "linux,initrd-start", start);
+        qemu_fdt_setprop_cell(fdt, "/chosen",
+                              "linux,initrd-end", start + size);
+    }
+
+    return start + size;
+}
+
+uint32_t openrisc_load_fdt(void *fdt, hwaddr load_start,
+                           uint64_t mem_size)
+{
+    uint32_t fdt_addr;
+    int ret;
+    int fdtsize = fdt_totalsize(fdt);
+
+    if (fdtsize <= 0) {
+        error_report("invalid device-tree");
+        exit(1);
+    }
+
+    /* We put fdt right after the kernel and/or initrd. */
+    fdt_addr = TARGET_PAGE_ALIGN(load_start);
+
+    ret = fdt_pack(fdt);
+    /* Should only fail if we've built a corrupted tree */
+    g_assert(ret == 0);
+    /* copy in the device tree */
+    qemu_fdt_dumpdtb(fdt, fdtsize);
+
+    rom_add_blob_fixed_as("fdt", fdt, fdtsize, fdt_addr,
+                          &address_space_memory);
+
+    return fdt_addr;
+}
+
hw/openrisc/meson.build +1 -0
index ec48172c9d..ab563820c5 100644
@@ -1,5 +1,6 @@
 openrisc_ss = ss.source_set()
 openrisc_ss.add(files('cputimer.c'))
+openrisc_ss.add(files('boot.c'))
 openrisc_ss.add(when: 'CONFIG_OR1K_SIM', if_true: [files('openrisc_sim.c'), fdt])
 
 hw_arch += {'openrisc': openrisc_ss}
hw/openrisc/openrisc_sim.c +6 -100

130 lines not on this page · raw message

include/hw/openrisc/boot.h new file +34 -0
new file mode 100644
index 0000000000..25a313d63a
@@ -0,0 +1,34 @@
+/*
+ * QEMU OpenRISC boot helpers.
+ *
+ * Copyright (c) 2022 Stafford Horne <[email protected]>
+ *
+ * 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 OPENRISC_BOOT_H
+#define OPENRISC_BOOT_H
+
+#include "exec/cpu-defs.h"
+
+hwaddr openrisc_load_kernel(ram_addr_t ram_size,
+                            const char *kernel_filename,
+                            uint32_t *bootstrap_pc);
+
+hwaddr openrisc_load_initrd(void *fdt, const char *filename,
+                            hwaddr load_start, uint64_t mem_size);
+
+uint32_t openrisc_load_fdt(void *fdt, hwaddr load_start,
+                           uint64_t mem_size);
+
+#endif /* OPENRISC_BOOT_H */
Signature · 1 line
2.31.1
Reviewed-by on v1 2/3
#6 lore raw

Re: [RFC PATCH 2/3] hw/openrisc: Split re-usable boot time apis out to boot.c

Quoted 11 lines · Stafford Horne

These will be shared with the virt platform.

---
  hw/openrisc/boot.c         | 127 +++++++++++++++++++++++++++++++++++++
  hw/openrisc/meson.build    |   1 +
  hw/openrisc/openrisc_sim.c | 106 ++-----------------------------
  include/hw/openrisc/boot.h |  34 ++++++++++
  4 files changed, 168 insertions(+), 100 deletions(-)

create mode 100644 hw/openrisc/boot.c
create mode 100644 include/hw/openrisc/boot.h

r~

#7 lore raw

RFC PATCH 3/3 hw/openrisc: Add the OpenRISC virtual machine

In reply to Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

The platform provides:
- OpenRISC SMP with up to 8 cpus
- A virtio bus with up to 8 devices
- Standard ns16550a serial
- Goldfish RTC
- SiFive TEST device for poweroff and reboot
- Generated RTC to automatically configure the guest kernel

---
 configs/devices/or1k-softmmu/default.mak |   1 +
 hw/openrisc/Kconfig                      |   9 +
 hw/openrisc/meson.build                  |   1 +
 hw/openrisc/virt.c                       | 429 +++++++++++++++++++++++
 4 files changed, 440 insertions(+)
 create mode 100644 hw/openrisc/virt.c
configs/devices/or1k-softmmu/default.mak +1 -0
index 5b3ac89491..f3bf816067 100644
@@ -5,3 +5,4 @@ CONFIG_SEMIHOSTING=y
 # Boards:
 #
 CONFIG_OR1K_SIM=y
+CONFIG_OR1K_VIRT=y
hw/openrisc/Kconfig +9 -0
index 8f284f3ba0..202134668e 100644
@@ -4,3 +4,12 @@ config OR1K_SIM
     select OPENCORES_ETH
     select OMPIC
     select SPLIT_IRQ
+
+config OR1K_VIRT
+    bool
+    imply VIRTIO_VGA
+    imply TEST_DEVICES
+    select GOLDFISH_RTC
+    select SERIAL
+    select SIFIVE_TEST
+    select VIRTIO_MMIO
hw/openrisc/meson.build +1 -0
index ab563820c5..2dbc6365bb 100644
@@ -2,5 +2,6 @@ openrisc_ss = ss.source_set()
 openrisc_ss.add(files('cputimer.c'))
 openrisc_ss.add(files('boot.c'))
 openrisc_ss.add(when: 'CONFIG_OR1K_SIM', if_true: [files('openrisc_sim.c'), fdt])
+openrisc_ss.add(when: 'CONFIG_OR1K_VIRT', if_true: [files('virt.c'), fdt])
 
 hw_arch += {'openrisc': openrisc_ss}
hw/openrisc/virt.c new file +429 -0

429 lines not on this page · raw message

Signature · 1 line
2.31.1
#8 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Hi Stafford,

Quoted 6 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

It's a good idea! I did some playing around with your patch today.

I'd suggest adding something to docs/system/target-openrsic.rst, including an example command lines.

Quoted 3 lines

The platform provides:
- OpenRISC SMP with up to 8 cpus

You have this:

#define VIRT_CPUS_MAX 4

I tried booting with -smp 4 and it locked up when starting userspace (or I stopped getting serial output?):

[    0.060000] smp: Brought up 1 node, 4 CPUs
...
[    0.960000] Run /init as init process

Running with -smp 2 and 3 worked. It does make booting much much slower.

Quoted 1 line

- A virtio bus with up to 8 devices

How do we go about adding a virtio-net-device to this bus?

I tried this:

$ ./qemu-system-or1k -M virt  -nographic -kernel
~/dev/kernels/shenki/or1ksim/vmlinux -initrd
~/dev/buildroot/openrisc/images/rootfs.cpio -device
virtio-net-device,netdev=net0 -netdev user,id=net0

I thought it wasn't working, but I just needed to enable the drivers, and from there it worked:

CONFIG_VIRTIO_NET=y
CONFIG_VIRTIO_MMIO=y

I also tested the virtio rng device which appeared to work.

# CONFIG_HW_RANDOM is not set
CONFIG_HW_RANDOM=y
CONFIG_HW_RANDOM_VIRTIO=y

Quoted 2 lines

- Standard ns16550a serial
- Goldfish RTC

-device virtio-rng-device,rng=rng0 -object rng-builtin,id=rng0

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

- .endianness = DEVICE_NATIVE_ENDIAN,
+ .endianness = DEVICE_HOST_ENDIAN,

[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)

But literally no other model in the tree does this, so I suspect it's not the right fix.

Quoted 1 line

- SiFive TEST device for poweroff and reboot

CONFIG_POWER_RESET=y
CONFIG_POWER_RESET_SYSCON=y
CONFIG_POWER_RESET_SYSCON_POWEROFF=y
CONFIG_SYSCON_REBOOT_MODE=y

Adding the syscon/mfd cruft to the kernel adds about 43KB just for rebooting. I guess that's okay as we're only dealing with a virtual platform.

Quoted 1 line

- Generated RTC to automatically configure the guest kernel

Did you mean device tree?

Quoted 26 lines
---
 configs/devices/or1k-softmmu/default.mak |   1 +
 hw/openrisc/Kconfig                      |   9 +
 hw/openrisc/meson.build                  |   1 +
 hw/openrisc/virt.c                       | 429 +++++++++++++++++++++++
 4 files changed, 440 insertions(+)
 create mode 100644 hw/openrisc/virt.c
configs/devices/or1k-softmmu/default.mak +1 -0
index 5b3ac89491..f3bf816067 100644
@@ -5,3 +5,4 @@ CONFIG_SEMIHOSTING=y
 # Boards:
 #
 CONFIG_OR1K_SIM=y
+CONFIG_OR1K_VIRT=y
hw/openrisc/Kconfig +0 -0
index 8f284f3ba0..202134668e 100644
@@ -4,3 +4,12 @@ config OR1K_SIM
     select OPENCORES_ETH
     select OMPIC

hw/openrisc/Kconfig config OR1K_SIM · line 15

Quoted 10 lines
hw/openrisc/Kconfig +9 -0
     select SPLIT_IRQ
+
+config OR1K_VIRT
+    bool
+    imply VIRTIO_VGA
+    imply TEST_DEVICES
+    select GOLDFISH_RTC
+    select SERIAL
+    select SIFIVE_TEST
+    select VIRTIO_MMIO

You could include the liteeth device too if we merged that.

Quoted 13 lines
hw/openrisc/virt.c new file +7 -0
new file mode 100644
index 0000000000..147196fda3
@@ -0,0 +1,429 @@
+/*
+ * OpenRISC QEMU virtual machine.
+ *
+ * Copyright (c) 2022 Stafford Horne <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public

hw/openrisc/virt.c line 17

Quoted 10 lines
hw/openrisc/virt.c new file +10 -0
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.

I think you can use the SPDX tag here instead of writing out the text.

Quoted 7 lines
hw/openrisc/virt.c new file +7 -0
+static void openrisc_virt_init(MachineState *machine)
+{
+    ram_addr_t ram_size = machine->ram_size;
+    const char *kernel_filename = machine->kernel_filename;
+    OpenRISCCPU *cpus[VIRT_CPUS_MAX] = {};
+    OR1KVirtState *state = VIRT_MACHINE(machine);
+    MemoryRegion *ram;

hw/openrisc/virt.c line 387

Quoted 10 lines
hw/openrisc/virt.c new file +9 -0
+    hwaddr load_addr;
+    int n;
+    unsigned int smp_cpus = machine->smp.cpus;
+
 
+    openrisc_virt_rtc_init(state, virt_memmap[VIRT_RTC].base,
+                           virt_memmap[VIRT_RTC].size, smp_cpus, cpus,
+                           VIRT_RTC_IRQ);
+
+    for (n = 0; n < VIRTIO_COUNT; n++) {

This would make more sense to me if you constructed the IRQ and base here, and then passed the actual base and irq number to your _virtio_init:

        size_t size = virt_memmap[VIRT_VIRTIO].size;
        openrisc_virt_virtio_init(state, virt_memmap[VIRT_VIRTIO].base
+ size * n,
                                  size, smp_cpus, cpus, VIRT_VIRTIO_IRQ + n);
Quoted 5 lines
hw/openrisc/virt.c new file +5 -0
+        openrisc_virt_virtio_init(state, virt_memmap[VIRT_VIRTIO].base,
+                                  virt_memmap[VIRT_VIRTIO].size,
+                                  smp_cpus, cpus, VIRT_VIRTIO_IRQ, n);
+    }
+
#9 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 23 lines · Joel Stanley

Hi Stafford,

Quoted 6 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

It's a good idea! I did some playing around with your patch today.

I'd suggest adding something to docs/system/target-openrsic.rst, including an example command lines.

Quoted 3 lines

The platform provides:
- OpenRISC SMP with up to 8 cpus

You have this:

#define VIRT_CPUS_MAX 4

Quoted 8 lines

I tried booting with -smp 4 and it locked up when starting userspace (or I stopped getting serial output?):

[    0.060000] smp: Brought up 1 node, 4 CPUs
...
[    0.960000] Run /init as init process

Running with -smp 2 and 3 worked. It does make booting much much slower.

target/openrisc/cpu.h is missing

#define TCG_GUEST_DEFAULT_MO (0)

to tell the JIT about the weakly ordered guest memory model, and to enable MTTCG by default.

Quoted 15 lines

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

(quoted diff) +1 -1
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,
Quoted 6 lines
[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)

But literally no other model in the tree does this, so I suspect it's not the right fix.

Correct. The model might require

.endianness = DEVICE_LITTLE_ENDIAN,

if that is the actual specification, or it may simply require fixes to handle a big-endian guest.

All that said, if we're going to make up a new virt platform, it should use PCI not virtio. See the recent discussion about RISC-V virtual machines, where they made exactly this mistake several years ago.

r~

#10 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 62 lines · Richard Henderson
Quoted 23 lines · Joel Stanley

Hi Stafford,

Quoted 6 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC.  This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

It's a good idea! I did some playing around with your patch today.

I'd suggest adding something to docs/system/target-openrsic.rst, including an example command lines.

Quoted 3 lines

The platform provides:
  - OpenRISC SMP with up to 8 cpus

You have this:

#define VIRT_CPUS_MAX 4

Quoted 8 lines

I tried booting with -smp 4 and it locked up when starting userspace (or I stopped getting serial output?):

[    0.060000] smp: Brought up 1 node, 4 CPUs
...
[    0.960000] Run /init as init process

Running with -smp 2 and 3 worked. It does make booting much much slower.

target/openrisc/cpu.h is missing

#define TCG_GUEST_DEFAULT_MO      (0)

to tell the JIT about the weakly ordered guest memory model, and to enable MTTCG by default.

Quoted 15 lines

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,

Quoted 6 lines

[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0 [    0.190000] goldfish_rtc 96005000.rtc: setting system clock to

2022-06-02T11:16:04 UTC (1654168564)

But literally no other model in the tree does this, so I suspect it's not the right fix.

Quoted 7 lines

Correct.  The model might require

    .endianness = DEVICE_LITTLE_ENDIAN,

if that is the actual specification, or it may simply require fixes to handle a big-endian guest.

All that said, if we're going to make up a new virt platform, it should use PCI not virtio.  See the recent discussion about RISC-V virtual machines, where they made exactly this mistake several years ago.

Unfortunately this precious remark was missed :(

#11 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 68 lines · Philippe Mathieu-Daudé
Quoted 67 lines · Richard Henderson
Quoted 23 lines · Joel Stanley

Hi Stafford,

Quoted 6 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC.  This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

It's a good idea! I did some playing around with your patch today.

I'd suggest adding something to docs/system/target-openrsic.rst, including an example command lines.

Quoted 3 lines

The platform provides:
  - OpenRISC SMP with up to 8 cpus

You have this:

#define VIRT_CPUS_MAX 4

Quoted 8 lines

I tried booting with -smp 4 and it locked up when starting userspace (or I stopped getting serial output?):

[    0.060000] smp: Brought up 1 node, 4 CPUs
...
[    0.960000] Run /init as init process

Running with -smp 2 and 3 worked. It does make booting much much slower.

target/openrisc/cpu.h is missing

#define TCG_GUEST_DEFAULT_MO      (0)

to tell the JIT about the weakly ordered guest memory model, and to enable MTTCG by default.

Quoted 15 lines

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,

Quoted 6 lines

[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0 [    0.190000] goldfish_rtc 96005000.rtc: setting system clock to

2022-06-02T11:16:04 UTC (1654168564)

But literally no other model in the tree does this, so I suspect it's not the right fix.

Correct.  The model might require

    .endianness = DEVICE_LITTLE_ENDIAN,

Quoted 8 lines
Quoted 6 lines

if that is the actual specification, or it may simply require fixes to handle a big-endian guest.

All that said, if we're going to make up a new virt platform, it should use PCI not virtio.  See the recent discussion about RISC-V virtual machines, where they made exactly this mistake several years ago.

Unfortunately this precious remark was missed :(

Which part of it? I think I took this comment into consideration and switched OpenRISC virt from virtio to PCI. But regarding endianness I did what I could.

-Stafford

#12 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

In reply to Joel Stanley

Hi Joel,

Quoted 22 lines · Joel Stanley
Quoted 5 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

(quoted diff) +1 -1
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,
Quoted 6 lines
[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)

But literally no other model in the tree does this, so I suspect it's not the right fix.

Goldfish devices are supposed to be little endian. Unfortunately m68k got this wrong, cfr.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2e2ac4a3327479f7e2744cdd88a5c823f2057bad
Please don't duplicate this bad behavior for new architectures.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#13 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 29 lines · Geert Uytterhoeven

Hi Joel,

Quoted 26 lines · Joel Stanley
Quoted 5 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

(quoted diff) +1 -1
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,
[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)
Quoted 7 lines
Quoted 2 lines

But literally no other model in the tree does this, so I suspect it's not the right fix.

Goldfish devices are supposed to be little endian. Unfortunately m68k got this wrong, cfr.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2e2ac4a3327479f7e2744cdd88a5c823f2057bad
Please don't duplicate this bad behavior for new architectures

Thanks for the pointer, I just wired in the goldfish RTC because I wanted to play with it. I was not attached to it. I can either remove it our find another RTC.

-Stafford

#14 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Hi Stafford,

Quoted 31 lines · Stafford Horne
Quoted 30 lines · Geert Uytterhoeven
Quoted 28 lines · Joel Stanley
Quoted 5 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

(quoted diff) +1 -1
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,
[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)

But literally no other model in the tree does this, so I suspect it's not the right fix.

Quoted 8 lines
Quoted 4 lines

Goldfish devices are supposed to be little endian. Unfortunately m68k got this wrong, cfr.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2e2ac4a3327479f7e2744cdd88a5c823f2057bad
Please don't duplicate this bad behavior for new architectures

Thanks for the pointer, I just wired in the goldfish RTC because I wanted to play with it. I was not attached to it. I can either remove it our find another RTC.

Sorry for being too unclear: the mistake was not to use the Goldfish RTC, but to make its register accesses big-endian.
Using Goldfish devices as little-endian devices should be fine.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#15 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 39 lines · Geert Uytterhoeven

Hi Stafford,

Quoted 36 lines · Stafford Horne
Quoted 34 lines · Geert Uytterhoeven
Quoted 22 lines · Joel Stanley
Quoted 5 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

(quoted diff) +1 -1
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,
Quoted 6 lines
[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)

But literally no other model in the tree does this, so I suspect it's not the right fix.

Goldfish devices are supposed to be little endian. Unfortunately m68k got this wrong, cfr.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2e2ac4a3327479f7e2744cdd88a5c823f2057bad
Please don't duplicate this bad behavior for new architectures

Quoted 7 lines
Quoted 3 lines

Thanks for the pointer, I just wired in the goldfish RTC because I wanted to play with it. I was not attached to it. I can either remove it our find another RTC.

Sorry for being too unclear: the mistake was not to use the Goldfish RTC, but to make its register accesses big-endian.
Using Goldfish devices as little-endian devices should be fine.

OK, then I would think this patch would be needed on Goldfish. I tested this out and it seems to work:

Patch:

hw/rtc/goldfish_rtc.c +0 -0
index 35e493be31..f1dc5af297 100644
@@ -219,7 +219,7 @@ static int goldfish_rtc_post_load(void *opaque, int

version_id)
static const MemoryRegionOps goldfish_rtc_ops = {
.read = goldfish_rtc_read,
.write = goldfish_rtc_write,
- .endianness = DEVICE_NATIVE_ENDIAN,
+ .endianness = DEVICE_LITTLE_ENDIAN,

     .valid = {
         .min_access_size = 4,
         .max_access_size = 4

Boot Log:

io scheduler mq-deadline registered
io scheduler kyber registered
Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled 90000000.serial: ttyS0 at MMIO 0x90000000 (irq = 2, base_baud = 1250000) is a 16550A
printk: console [ttyS0] enabled
loop: module loaded
virtio_blk virtio1: [vda] 32768 512-byte logical blocks (16.8 MB/16.0 MiB) Freeing initrd memory: 1696K
*goldfish_rtc 96005000.rtc: registered as rtc0
*goldfish_rtc 96005000.rtc: setting system clock to 2022-06-05T01:49:57 UTC (1654393797)
NET: Registered PF_PACKET protocol family
random: fast init done

-Stafford

#16 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 44 lines · Stafford Horne
Quoted 43 lines · Geert Uytterhoeven

Hi Stafford,

Quoted 39 lines · Stafford Horne
Quoted 27 lines · Geert Uytterhoeven
Quoted 26 lines · Joel Stanley
Quoted 5 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

(quoted diff) +1 -1
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,
[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)
Quoted 7 lines
Quoted 2 lines

But literally no other model in the tree does this, so I suspect it's not the right fix.

Goldfish devices are supposed to be little endian. Unfortunately m68k got this wrong, cfr.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2e2ac4a3327479f7e2744cdd88a5c823f2057bad
Please don't duplicate this bad behavior for new architectures

Thanks for the pointer, I just wired in the goldfish RTC because I wanted to play with it. I was not attached to it. I can either remove it our find another RTC.

Quoted 6 lines
Quoted 3 lines

Sorry for being too unclear: the mistake was not to use the Goldfish RTC, but to make its register accesses big-endian.
Using Goldfish devices as little-endian devices should be fine.

OK, then I would think this patch would be needed on Goldfish. I tested this out and it seems to work:

Sorry, it seems maybe I mis-understood this again. In Arnd's mail [1] he, at the end, mentions.

It might be a good idea to revisit the qemu implementation and make sure that the extra byteswap is only inserted on m68k and not on other targets, but hopefully there are no new targets based on goldfish anymore and we don't need to care.

So, it seems that in addition to my patch we would need something in m68k to switch it back to 'native' (big) endian?

Looking at the m68k kernel/qemu interface I see:

Pre 5.19:
(data) <-- kernel(readl / little) <-- m68k qemu (native / big) - RTC/PIC (data) <-- kernel(__raw_readl / big) <-- m68k qemu (native / big) - TTY

5.19:
(data) <-- kernel(gf_ioread32 / big) <-- m68k qemu (native / big) - all

The new fixes to add gf_ioread32/gf_iowrite32 fix this for goldfish and m68k. This wouldn't have been an issue for little-endian platforms where readl/writel were originally used.

Why can't m68k switch to little-endian in qemu and the kernel? The m68k virt platform is not that old, 1 year? Are there a lot of users that this would be a big problem?

[1] https://lore.kernel.org/lkml/CAK8P3a1oN8NrUjkh2X8jHQbyz42Xo6GSa=5n0gD6vQcXRjmq1Q@mail.gmail.com/

-Stafford

Quoted 33 lines

Patch:

hw/rtc/goldfish_rtc.c +1 -1
index 35e493be31..f1dc5af297 100644
@@ -219,7 +219,7 @@ static int goldfish_rtc_post_load(void *opaque, int
 version_id)
 static const MemoryRegionOps goldfish_rtc_ops = {
     .read = goldfish_rtc_read,
     .write = goldfish_rtc_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_LITTLE_ENDIAN,
     .valid = {
         .min_access_size = 4,

.max_access_size = 4

Boot Log:

io scheduler mq-deadline registered
io scheduler kyber registered
Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled 90000000.serial: ttyS0 at MMIO 0x90000000 (irq = 2, base_baud = 1250000) is a 16550A
printk: console [ttyS0] enabled
loop: module loaded
virtio_blk virtio1: [vda] 32768 512-byte logical blocks (16.8 MB/16.0 MiB) Freeing initrd memory: 1696K
*goldfish_rtc 96005000.rtc: registered as rtc0
*goldfish_rtc 96005000.rtc: setting system clock to 2022-06-05T01:49:57 UTC (1654393797)
NET: Registered PF_PACKET protocol family
random: fast init done

-Stafford

#17 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Hi folks,

Quoted 3 lines · Stafford Horne

Why can't m68k switch to little-endian in qemu and the kernel? The m68k virt platform is not that old, 1 year? Are there a lot of users that this would be a big problem?

I also share this perspective. AFAICT, m68k virt platform *just* shipped. Fix this stuff instead of creating more compatibility bloat for a platform with no new silicon. The risks of making life difficult for 15 minutes for all seven and a half users of that code that only now has become operational is vastly dwarfed by the good sense to just fix the mistake. Treat the endian thing as a *bug* rather than a sacred ABI. Bugs only become sacred if you let them sit for years and large numbers of people grow to rely on spacebar heating. Otherwise they're just bugs. This can be fixed.

Jason

#18 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

+ Arnd

Quoted 19 lines · Jason A. Donenfeld

Hi folks,

Quoted 3 lines · Stafford Horne

Why can't m68k switch to little-endian in qemu and the kernel? The m68k virt platform is not that old, 1 year? Are there a lot of users that this would be a big problem?

I also share this perspective. AFAICT, m68k virt platform *just* shipped. Fix this stuff instead of creating more compatibility bloat for a platform with no new silicon. The risks of making life difficult for 15 minutes for all seven and a half users of that code that only now has become operational is vastly dwarfed by the good sense to just fix the mistake. Treat the endian thing as a *bug* rather than a sacred ABI. Bugs only become sacred if you let them sit for years and large numbers of people grow to rely on spacebar heating. Otherwise they're just bugs. This can be fixed.

Jason

#19 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

In reply to Stafford Horne

CC arnd

Quoted 115 lines · Stafford Horne
Quoted 42 lines · Stafford Horne
Quoted 41 lines · Geert Uytterhoeven
Quoted 39 lines · Stafford Horne
Quoted 27 lines · Geert Uytterhoeven
Quoted 26 lines · Joel Stanley
Quoted 5 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Much of this has been sourced from the m68k and riscv virt platforms.

I enabled the options:

CONFIG_RTC_CLASS=y
# CONFIG_RTC_SYSTOHC is not set
# CONFIG_RTC_NVMEM is not set
CONFIG_RTC_DRV_GOLDFISH=y

But it didn't work. It seems the goldfish rtc model doesn't handle a big endian guest running on my little endian host.

Doing this fixes it:

(quoted diff) +1 -1
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_HOST_ENDIAN,
[    0.190000] goldfish_rtc 96005000.rtc: registered as rtc0
[    0.190000] goldfish_rtc 96005000.rtc: setting system clock to
2022-06-02T11:16:04 UTC (1654168564)
Quoted 7 lines
Quoted 2 lines

But literally no other model in the tree does this, so I suspect it's not the right fix.

Goldfish devices are supposed to be little endian. Unfortunately m68k got this wrong, cfr.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2e2ac4a3327479f7e2744cdd88a5c823f2057bad
Please don't duplicate this bad behavior for new architectures

Thanks for the pointer, I just wired in the goldfish RTC because I wanted to play with it. I was not attached to it. I can either remove it our find another RTC.

Quoted 6 lines
Quoted 3 lines

Sorry for being too unclear: the mistake was not to use the Goldfish RTC, but to make its register accesses big-endian.
Using Goldfish devices as little-endian devices should be fine.

OK, then I would think this patch would be needed on Goldfish. I tested this out and it seems to work:

Sorry, it seems maybe I mis-understood this again. In Arnd's mail [1] he, at the end, mentions.

It might be a good idea to revisit the qemu implementation and make sure that the extra byteswap is only inserted on m68k and not on other targets, but hopefully there are no new targets based on goldfish anymore and we don't need to care.

So, it seems that in addition to my patch we would need something in m68k to switch it back to 'native' (big) endian?

Looking at the m68k kernel/qemu interface I see:

Pre 5.19:
(data) <-- kernel(readl / little) <-- m68k qemu (native / big) - RTC/PIC (data) <-- kernel(__raw_readl / big) <-- m68k qemu (native / big) - TTY

5.19:

   (data) <-- kernel(gf_ioread32 / big) <-- m68k qemu (native / big) - all

The new fixes to add gf_ioread32/gf_iowrite32 fix this for goldfish and m68k. This wouldn't have been an issue for little-endian platforms where readl/writel were originally used.

Why can't m68k switch to little-endian in qemu and the kernel? The m68k virt platform is not that old, 1 year? Are there a lot of users that this would be a big problem?

[1] https://lore.kernel.org/lkml/CAK8P3a1oN8NrUjkh2X8jHQbyz42Xo6GSa=5n0gD6vQcXRjmq1Q@mail.gmail.com/

-Stafford

Quoted 33 lines

Patch:

hw/rtc/goldfish_rtc.c +1 -1
index 35e493be31..f1dc5af297 100644
@@ -219,7 +219,7 @@ static int goldfish_rtc_post_load(void *opaque, int
 version_id)
 static const MemoryRegionOps goldfish_rtc_ops = {
     .read = goldfish_rtc_read,
     .write = goldfish_rtc_write,
-    .endianness = DEVICE_NATIVE_ENDIAN,
+    .endianness = DEVICE_LITTLE_ENDIAN,
     .valid = {
         .min_access_size = 4,

.max_access_size = 4

Boot Log:

io scheduler mq-deadline registered
io scheduler kyber registered
Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled 90000000.serial: ttyS0 at MMIO 0x90000000 (irq = 2, base_baud = 1250000) is a 16550A
printk: console [ttyS0] enabled
loop: module loaded
virtio_blk virtio1: [vda] 32768 512-byte logical blocks (16.8 MB/16.0 MiB) Freeing initrd memory: 1696K
*goldfish_rtc 96005000.rtc: registered as rtc0
*goldfish_rtc 96005000.rtc: setting system clock to 2022-06-05T01:49:57 UTC (1654393797)
NET: Registered PF_PACKET protocol family
random: fast init done

-Stafford

#20 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 23 lines · Geert Uytterhoeven
Quoted 22 lines · Stafford Horne

On Sun, Jun 05, 2022 at 10:58:14AM +0900, Stafford Horne wrote:
It might be a good idea to revisit the qemu implementation and make sure that the extra byteswap is only inserted on m68k and not on other targets, but hopefully there are no new targets based on goldfish anymore and we don't need to care.

So, it seems that in addition to my patch we would need something in m68k to switch it back to 'native' (big) endian?

Looking at the m68k kernel/qemu interface I see:

Pre 5.19:
(data) <-- kernel(readl / little) <-- m68k qemu (native / big) - RTC/PIC (data) <-- kernel(__raw_readl / big) <-- m68k qemu (native / big) - TTY

5.19:

   (data) <-- kernel(gf_ioread32 / big) <-- m68k qemu (native / big) - all

The new fixes to add gf_ioread32/gf_iowrite32 fix this for goldfish and m68k. This wouldn't have been an issue for little-endian platforms where readl/writel were originally used.

Quoted 5 lines
Quoted 5 lines

Why can't m68k switch to little-endian in qemu and the kernel? The m68k virt platform is not that old, 1 year? Are there a lot of users that this would be a big problem?

[1] https://lore.kernel.org/lkml/CAK8P3a1oN8NrUjkh2X8jHQbyz42Xo6GSa=5n0gD6vQcXRjmq1Q@mail.gmail.com/

Goldfish is a very old platform, as far as I know only the kernel port is new. I don't know when qemu started shipping goldfish, but changing it now would surely break compatibility with whatever OS the port was originally made for.

Arnd

#21 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 28 lines · Arnd Bergmann
Quoted 27 lines · Geert Uytterhoeven
Quoted 26 lines · Stafford Horne

On Sun, Jun 05, 2022 at 10:58:14AM +0900, Stafford Horne wrote:
It might be a good idea to revisit the qemu implementation and make sure that the extra byteswap is only inserted on m68k and not on other targets, but hopefully there are no new targets based on goldfish anymore and we don't need to care.

So, it seems that in addition to my patch we would need something in m68k to switch it back to 'native' (big) endian?

Looking at the m68k kernel/qemu interface I see:

Pre 5.19:
(data) <-- kernel(readl / little) <-- m68k qemu (native / big) - RTC/PIC (data) <-- kernel(__raw_readl / big) <-- m68k qemu (native / big) - TTY

5.19:

   (data) <-- kernel(gf_ioread32 / big) <-- m68k qemu (native / big) - all

The new fixes to add gf_ioread32/gf_iowrite32 fix this for goldfish and m68k. This wouldn't have been an issue for little-endian platforms where readl/writel were originally used.

Why can't m68k switch to little-endian in qemu and the kernel? The m68k virt platform is not that old, 1 year? Are there a lot of users that this would be a big problem?

Quoted 5 lines
Quoted 1 line
Quoted 1 line

[1] https://lore.kernel.org/lkml/CAK8P3a1oN8NrUjkh2X8jHQbyz42Xo6GSa=5n0gD6vQcXRjmq1Q@mail.gmail.com/

Goldfish is a very old platform, as far as I know only the kernel port is new. I don't know when qemu started shipping goldfish, but changing it now would surely break compatibility with whatever OS the port was originally made for.

Hi Arnd,

As far as I can tell goldfish in qemu is not very old. There are 3 devices, 2 were added for the m68k virt machine, and 1 for riscv virt.

    $ git lo -- hw/rtc/goldfish_rtc.c
    2022-01-28 2f93d8b04a Peter Maydell    rtc: Move RTC function prototypes to their own header 
    2021-03-04 6b9409ba5f Laurent Vivier   goldfish_rtc: re-arm the alarm after migration 
    2020-10-13 16b66c5626 Laurent Vivier   goldfish_rtc: change MemoryRegionOps endianness to DEVICE_NATIVE_ENDIAN 
    2020-07-22 8380b3a453 Jessica Clarke   goldfish_rtc: Fix non-atomic read behaviour of TIME_LOW/TIME_HIGH 
    2020-02-10 9a5b40b842 Anup Patel       hw: rtc: Add Goldfish RTC device 
    $ git lo -- hw/char/goldfish_tty.c
    2021-11-09 65b4c8c759 Philippe Mathieu-Daudé hw/m68k: Fix typo in SPDX tag 
    2021-03-15 8c6df16ff6 Laurent Vivier   hw/char: add goldfish-tty 
    $  git lo -- hw/intc/goldfish_pic.c
    2021-11-09 65b4c8c759 Philippe Mathieu-Daudé hw/m68k: Fix typo in SPDX tag 
    2021-03-15 8785559390 Laurent Vivier   hw/intc: add goldfish-pic 

The mips/loongson3_virt machine now also uses the goldfish_rtc.

The problem with the goldfish device models is that they were defined as DEVICE_NATIVE_ENDIAN.

    $ grep endianness hw/*/goldfish*
    hw/char/goldfish_tty.c:    .endianness = DEVICE_NATIVE_ENDIAN,
    hw/intc/goldfish_pic.c:    .endianness = DEVICE_NATIVE_ENDIAN,
    hw/rtc/goldfish_rtc.c:    .endianness = DEVICE_NATIVE_ENDIAN,

RISC-V is little-endian so when it was added there was no problem with running linux goldfish drivers.

MIPS Longson3, added last year, seems to be running as little-endian well, I understand MIPS can support both big and little endian. However according to this all Loongson cores are little-endian.

https://en.wikipedia.org/wiki/Loongson

As I understand when endianness of the devices in qemu are defined as DEVICE_NATIVE_ENDIAN the device endian takes the endian of the target CPU. This means that MIPS Loongson3 and RISC-V are affectively running as little-endian which is what would be expected.

So it appears to me that in qemu that m68k is the only architecture that is providing goldfish devices on a big-endian architecture. Also, as far as I know Linux is the only OS that was updated to cater for that. If there are other firmware/bootloaders that expect that maybe they could be updated too?

-Stafford

#22 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 11 lines · Stafford Horne
Quoted 3 lines · Arnd Bergmann

Goldfish is a very old platform, as far as I know only the kernel port is new. I don't know when qemu started shipping goldfish, but changing it now would surely break compatibility with whatever OS the port was originally made for.

Hi Arnd,

As far as I can tell goldfish in qemu is not very old. There are 3 devices, 2 were added for the m68k virt machine, and 1 for riscv virt.

Quoted 7 lines
    $ git lo -- hw/char/goldfish_tty.c
    2021-11-09 65b4c8c759 Philippe Mathieu-Daudé hw/m68k: Fix typo in SPDX tag
    2021-03-15 8c6df16ff6 Laurent Vivier   hw/char: add goldfish-tty
    $  git lo -- hw/intc/goldfish_pic.c
    2021-11-09 65b4c8c759 Philippe Mathieu-Daudé hw/m68k: Fix typo in SPDX tag
    2021-03-15 8785559390 Laurent Vivier   hw/intc: add goldfish-pic

That is much younger than Laurent made it appear, from his earlier explanations I expected this to have shipped a long time ago and been used in other OSs to the
point where it cannot be fixed.

Quoted 17 lines

The mips/loongson3_virt machine now also uses the goldfish_rtc.

The problem with the goldfish device models is that they were defined as DEVICE_NATIVE_ENDIAN.

    $ grep endianness hw/*/goldfish*
    hw/char/goldfish_tty.c:    .endianness = DEVICE_NATIVE_ENDIAN,
    hw/intc/goldfish_pic.c:    .endianness = DEVICE_NATIVE_ENDIAN,
    hw/rtc/goldfish_rtc.c:    .endianness = DEVICE_NATIVE_ENDIAN,

RISC-V is little-endian so when it was added there was no problem with running linux goldfish drivers.

MIPS Longson3, added last year, seems to be running as little-endian well, I understand MIPS can support both big and little endian. However according to this all Loongson cores are little-endian.

Quoted 7 lines

https://en.wikipedia.org/wiki/Loongson

As I understand when endianness of the devices in qemu are defined as DEVICE_NATIVE_ENDIAN the device endian takes the endian of the target CPU.

This means that MIPS Loongson3 and RISC-V are affectively running as little-endian which is what would be expected.

Not really, the definition of DEVICE_NATIVE_ENDIAN in qemu is much less well-defined than that as I understand, it is just whatever the person adding support for that CPU thought would be the right one. A lot of CPUs can run either big-endian or little-endian code, and e.g. on ARM, qemu DEVICE_NATIVE_ENDIAN is just always little-endian, regardless of what the CPU runs, while I think on MIPS it would be whatever the CPU is actually executing.

Arnd

#23 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

In reply to Stafford Horne
Quoted 10 lines · Stafford Horne
Quoted 3 lines · Arnd Bergmann

Goldfish is a very old platform, as far as I know only the kernel port is new. I don't know when qemu started shipping goldfish, but changing it now would surely break compatibility with whatever OS the port was originally made for.

Hi Arnd,

As far as I can tell goldfish in qemu is not very old. There are 3 devices, 2 were added for the m68k virt machine, and 1 for riscv virt.

Yep, these are new for (upstream) QEMU, and AIUI the only OS we care about for these is Linux, really. My understanding is that these devices were added because they were conveniently available in Linux :-) Where they do have a much older history is in the Android emulator, but upstream QEMU doesn't care about that.

Quoted 15 lines

The problem with the goldfish device models is that they were defined as DEVICE_NATIVE_ENDIAN.

    $ grep endianness hw/*/goldfish*
    hw/char/goldfish_tty.c:    .endianness = DEVICE_NATIVE_ENDIAN,
    hw/intc/goldfish_pic.c:    .endianness = DEVICE_NATIVE_ENDIAN,
    hw/rtc/goldfish_rtc.c:    .endianness = DEVICE_NATIVE_ENDIAN,

RISC-V is little-endian so when it was added there was no problem with running linux goldfish drivers.

MIPS Longson3, added last year, seems to be running as little-endian well, I understand MIPS can support both big and little endian. However according to this all Loongson cores are little-endian.

Quoted 6 lines

https://en.wikipedia.org/wiki/Loongson

As I understand when endianness of the devices in qemu are defined as DEVICE_NATIVE_ENDIAN the device endian takes the endian of the target CPU. This means that MIPS Loongson3 and RISC-V are affectively running as little-endian which is what would be expected.

DEVICE_NATIVE_ENDIAN means "whatever the 'native' endianness of the target CPU architecture is". This is a compile-time thing, and doesn't change if the CPU changes its endianness at runtime. So for instance for Arm boards DEVICE_NATIVE_ENDIAN and DEVICE_LITTLE_ENDIAN are the same thing, even if the guest OS is running with SCTLR_EL1.EE set (and even if a particular board in qemu-system-arm sets up the CPU so it leaves reset with .EE set to 1) The analogy on real hardware is that the way these "switch endianness" CPUs work is that they just flip the bytes in the data on their way out of the CPU, so changing the endianness in the CPU doesn't cause devices to change the way they behave. QEMU's "target endianness" is kind of like a property of the interconnect/system design in some ways.

From QEMU's point of view, the thing we really don't want is devices that magically change behaviour when the CPU switches endianness at runtime, because those are weirdly unlike real hardware. (Virtio is the main offender in this regard, but we're stuck with that.) Devices that happen to be wired up differently on different target architectures are fine for us. I don't have any definite examples to hand, but my understanding is that this happens with real hardware too, where a device (maybe 8250-compatible UART or Lance ethernet are examples?) with 32-bit registers might be typically wired up in a system for a big-endian CPU such that the guest code can write a 32-bit word to it and get the "obvious" ordering matching the device datasheet. This sort of thing is what DEVICE_NATIVE_ENDIAN was intended for. (There are also various places where we use it where perhaps we should not where a device is exclusively used on a CPU of a particular endianness, and so you could equally write DEVICE_LITTLE_ENDIAN or whatever without any behaviour change.)

So I don't have a strong view on whether these devices should be DEVICE_NATIVE_ENDIAN or DEVICE_LITTLE_ENDIAN (except that my impression is that a DEVICE_LITTLE_ENDIAN device on a big-endian system is a bit weird, because it means the guest has to byteswap everything. You see that with PCI devices because the PCI spec mandates LE, but not often elsewhere).

If there's an official-ish spec for how goldfish devices are supposed to behave (does anybody have a pointer to one?) and it says "always little-endian" then that would probably suggest that fixing m68k would be nice if we can.

thanks
-- PMM

#24 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 11 lines · Peter Maydell

So I don't have a strong view on whether these devices should be DEVICE_NATIVE_ENDIAN or DEVICE_LITTLE_ENDIAN (except that my impression is that a DEVICE_LITTLE_ENDIAN device on a big-endian system is a bit weird, because it means the guest has to byteswap everything. You see that with PCI devices because the PCI spec mandates LE, but not often elsewhere).

If there's an official-ish spec for how goldfish devices are supposed to behave (does anybody have a pointer to one?) and it says "always little-endian" then that would probably suggest that fixing m68k would be nice if we can.

I think there are some conflicting thoughts on this.

In Geert's he mentioned:

Using Goldfish devices as little-endian devices should be fine.

In Arnd's mail he mentions:

https://lore.kernel.org/lkml/CAK8P3a1oN8NrUjkh2X8jHQbyz42Xo6GSa=5n0gD6vQcXRjmq1Q@mail.gmail.com/#t

... the device was clearly defined as having little-endian registers,

Based on that I was thinking that switching to DEVICE_LITTLE_ENDIAN would make sense.

However, in a followup mail from Laurent we see:

https://lore.kernel.org/lkml/[email protected]/

The reference document[1] doesn't define the endianness of goldfish.

[1] https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT

The documentation does not clearly specify it. So maybe maybe or1k should just be updated on the linux side and add gf_ioread32/gf_iowrite32 big-endian accessors.

-Stafford

#25 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 6 lines · Stafford Horne

On Tue, Jun 07, 2022 at 11:43:08AM +0100, Peter Maydell wrote:

However, in a followup mail from Laurent we see:

https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

The reference document[1] doesn't define the endianness of goldfish.

[1] https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT

The documentation does not clearly specify it. So maybe maybe or1k should just be updated on the linux side and add gf_ioread32/gf_iowrite32 big-endian accessors.

I don't think it makes any sense to use big-endian for a new architecture, just use
the default little-endian implementation on the linux side, and change the qemu code
to have the backward-compatibility hack for m68k while using big-endian for the rest.

Arnd

#26 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 16 lines · Arnd Bergmann
Quoted 6 lines · Stafford Horne

On Tue, Jun 07, 2022 at 11:43:08AM +0100, Peter Maydell wrote:

However, in a followup mail from Laurent we see:

https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

The reference document[1] doesn't define the endianness of goldfish.

[1] https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT

The documentation does not clearly specify it. So maybe maybe or1k should just be updated on the linux side and add gf_ioread32/gf_iowrite32 big-endian accessors.

Quoted 6 lines

I don't think it makes any sense to use big-endian for a new architecture, just use
the default little-endian implementation on the linux side, and change the qemu code
to have the backward-compatibility hack for m68k while using big-endian for the rest.

Hitting this thread 3 years latter, suffering with endiannes. Sigh.

Back to OpenRISC virt machine, it is unfortunate it picked the TYPE_SIFIVE_TEST virtual device (expected to be little-endian) instead of the TYPE_VIRT_CTRL one (expected to be big-endian, like OpenRISC).

Fortunately (to me) OpenRISC virt machine exposes the TYPE_SIFIVE_TEST virtual device via device tree, and make its endianness explicitly to little order.

Stafford, is it too late to use the TYPE_VIRT_CTRL? We could map it as a secondary reset device at another address, deprecate the use of the mapped SIFIVE_TEST then remove it.

#27 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 16 lines · Arnd Bergmann
Quoted 14 lines · Stafford Horne

On Tue, Jun 07, 2022 at 11:43:08AM +0100, Peter Maydell wrote:

However, in a followup mail from Laurent we see:

https://lore.kernel.org/lkml/[email protected]/

The reference document[1] doesn't define the endianness of goldfish.

[1] https://android.googlesource.com/platform/external/qemu/+/master/docs/GOLDFISH-VIRTUAL-HARDWARE.TXT

The documentation does not clearly specify it. So maybe maybe or1k should just be updated on the linux side and add gf_ioread32/gf_iowrite32 big-endian accessors.

Quoted 8 lines
Quoted 6 lines

I don't think it makes any sense to use big-endian for a new architecture, just use
the default little-endian implementation on the linux side, and change the qemu code
to have the backward-compatibility hack for m68k while using big-endian for the rest.

Hitting this thread 3 years latter, suffering with endiannes. Sigh.

What is the issue this is causing?

Quoted 5 lines

Back to OpenRISC virt machine, it is unfortunate it picked the TYPE_SIFIVE_TEST virtual device (expected to be little-endian) instead of the TYPE_VIRT_CTRL one (expected to be big-endian, like OpenRISC).

Quoted 7 lines

Fortunately (to me) OpenRISC virt machine exposes the TYPE_SIFIVE_TEST virtual device via device tree, and make its endianness explicitly to little order.

Stafford, is it too late to use the TYPE_VIRT_CTRL? We could map it as a secondary reset device at another address, deprecate the use of the mapped SIFIVE_TEST then remove it.

I think its ok to add a second device if that will help you. But, it will take time to deco the old device as people may be running old kernels, with old device trees that expect the SIFIVE device for some time.

-Stafford

#28 lore raw

Re: [RFC PATCH 3/3] hw/openrisc: Add the OpenRISC virtual machine

In reply to Joel Stanley
Quoted 8 lines · Joel Stanley

Hi Stafford,

Quoted 5 lines · Stafford Horne

This patch add the OpenRISC virtual machine 'virt' for OpenRISC. This platform allows for a convenient CI platform for toolchain, software ports and the OpenRISC linux kernel port.

Quoted 6 lines
Quoted 1 line

Much of this has been sourced from the m68k and riscv virt platforms.

It's a good idea! I did some playing around with your patch today.

I'd suggest adding something to docs/system/target-openrsic.rst, including an example command lines.

Yeah, good idea, this is the command I am using:

qemu-system-or1k -cpu or1200 -M virt \
-kernel /home/shorne/work/linux/vmlinux \
-initrd /home/shorne/work/linux/initramfs.cpio.gz \
-device virtio-net-device,netdev=user -netdev user,id=user,net=10.9.0.1/24,host=10.9.0.100 \
-serial mon:stdio -nographic \
-device virtio-blk-device,drive=d0 -drive file=/home/shorne/work/linux/virt.qcow2,id=d0,if=none,format=qcow2 \
-gdb tcp::10001 -smp cpus=2 -m 64

I should have mentioned it but the config I am using is here:

https://github.com/stffrdhrn/linux/commits/or1k-virt

Quoted 11 lines
Quoted 3 lines

The platform provides:
- OpenRISC SMP with up to 8 cpus

You have this:

#define VIRT_CPUS_MAX 4
i
I tried booting with -smp 4 and it locked up when starting userspace (or I stopped getting serial output?):

Quoted 5 lines
[    0.060000] smp: Brought up 1 node, 4 CPUs
...
[    0.960000] Run /init as init process

Running with -smp 2 and 3 worked. It does make booting much much slower.

Right, it should be 4, I just write 8 from memory. You are also, right I have issues with running 4 CPU's. I will try richard's suggestion. I have some old patches to configure MTTCG also, but it had some limitations. I will dig those up and get this fixed for this series.

Quoted 3 lines
Quoted 1 line

- Generated RTC to automatically configure the guest kernel

Did you mean device tree?

Yeah, thats what I meant.

Quoted 30 lines
Quoted 30 lines
---
 configs/devices/or1k-softmmu/default.mak |   1 +
 hw/openrisc/Kconfig                      |   9 +
 hw/openrisc/meson.build                  |   1 +
 hw/openrisc/virt.c                       | 429 +++++++++++++++++++++++
 4 files changed, 440 insertions(+)
 create mode 100644 hw/openrisc/virt.c
configs/devices/or1k-softmmu/default.mak +1 -0
index 5b3ac89491..f3bf816067 100644
@@ -5,3 +5,4 @@ CONFIG_SEMIHOSTING=y
 # Boards:
 #
 CONFIG_OR1K_SIM=y
+CONFIG_OR1K_VIRT=y
hw/openrisc/Kconfig +3 -0
index 8f284f3ba0..202134668e 100644
@@ -4,3 +4,12 @@ config OR1K_SIM
     select OPENCORES_ETH
     select OMPIC
     select SPLIT_IRQ
+
+config OR1K_VIRT
+    bool
Quoted 8 lines

hw/openrisc/Kconfig config OR1K_SIM · line 15

Quoted 6 lines
hw/openrisc/Kconfig +6 -0
+    imply VIRTIO_VGA
+    imply TEST_DEVICES
+    select GOLDFISH_RTC
+    select SERIAL
+    select SIFIVE_TEST
+    select VIRTIO_MMIO

You could include the liteeth device too if we merged that.

I think we could add that with a litex machine. For that we would need at least the litex UART and SoC for reset.

Quoted 17 lines
Quoted 17 lines
hw/openrisc/virt.c new file +11 -0
new file mode 100644
index 0000000000..147196fda3
@@ -0,0 +1,429 @@
+/*
+ * OpenRISC QEMU virtual machine.
+ *
+ * Copyright (c) 2022 Stafford Horne <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
Quoted 8 lines

hw/openrisc/virt.c line 17

Quoted 6 lines
hw/openrisc/virt.c new file +6 -0
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.

I think you can use the SPDX tag here instead of writing out the text.

Right.

Quoted 18 lines
Quoted 7 lines
hw/openrisc/virt.c new file +7 -0
+static void openrisc_virt_init(MachineState *machine)
+{
+    ram_addr_t ram_size = machine->ram_size;
+    const char *kernel_filename = machine->kernel_filename;
+    OpenRISCCPU *cpus[VIRT_CPUS_MAX] = {};
+    OR1KVirtState *state = VIRT_MACHINE(machine);
+    MemoryRegion *ram;

hw/openrisc/virt.c line 387

Quoted 10 lines
hw/openrisc/virt.c new file +9 -0
+    hwaddr load_addr;
+    int n;
+    unsigned int smp_cpus = machine->smp.cpus;
+
 
+    openrisc_virt_rtc_init(state, virt_memmap[VIRT_RTC].base,
+                           virt_memmap[VIRT_RTC].size, smp_cpus, cpus,
+                           VIRT_RTC_IRQ);
+
+    for (n = 0; n < VIRTIO_COUNT; n++) {
Quoted 9 lines

This would make more sense to me if you constructed the IRQ and base here, and then passed the actual base and irq number to your _virtio_init:

        size_t size = virt_memmap[VIRT_VIRTIO].size;
        openrisc_virt_virtio_init(state, virt_memmap[VIRT_VIRTIO].base
+ size * n,
                                  size, smp_cpus, cpus, VIRT_VIRTIO_IRQ + n);

OK, yes that is better.

Quoted 5 lines

hw/openrisc/virt.c line 392

Quoted 5 lines
hw/openrisc/virt.c new file +5 -0
+        openrisc_virt_virtio_init(state, virt_memmap[VIRT_VIRTIO].base,
+                                  virt_memmap[VIRT_VIRTIO].size,
+                                  smp_cpus, cpus, VIRT_VIRTIO_IRQ, n);
+    }
+

Thanks a lot for the review.

-Stafford