Skip to contentSkip to conversation

hw/riscv/spike: Replace tswap64() by ldq_endian_p()

PATCH In review Philippe Mathieu-Daudé 3 participants 5 messages Sep 30, 2024 – Oct 3, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/riscv/spike: Replace tswap64() by ldq_endian_p()

Hold the target endianness in HTIFState::target_is_bigendian. Pass the target endianness as argument to htif_mm_init(). Replace tswap64() calls by ldq_endian_p() ones.

---
Based-on: <[email protected]>
"qemu/bswap: Introduce ld/st_endian_p() API"

Note: this is a non-QOM device!

---
 include/hw/char/riscv_htif.h |  4 +++-
 hw/char/riscv_htif.c         | 17 +++++++++++------
 hw/riscv/spike.c             |  2 +-
 3 files changed, 15 insertions(+), 8 deletions(-)
include/hw/char/riscv_htif.h +3 -1
index df493fdf6b..24868ddfe1 100644
@@ -35,6 +35,7 @@ typedef struct HTIFState {
     hwaddr tohost_offset;
     hwaddr fromhost_offset;
     MemoryRegion mmio;
+    bool target_is_bigendian;
 
     CharBackend chr;
     uint64_t pending_read;
@@ -49,6 +50,7 @@ void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value,
 
 /* legacy pre qom */
 HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base);
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian);
 
 #endif
hw/char/riscv_htif.c +11 -6
index 9bef60def1..77951f3c76 100644
@@ -30,7 +30,6 @@
 #include "qemu/timer.h"
 #include "qemu/error-report.h"
 #include "exec/address-spaces.h"
-#include "exec/tswap.h"
 #include "sysemu/dma.h"
 #include "sysemu/runstate.h"
 
@@ -211,13 +210,17 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
                     SHUTDOWN_CAUSE_GUEST_SHUTDOWN, exit_code);
                 return;
             } else {
+                bool be = s->target_is_bigendian;
                 uint64_t syscall[8];
+
                 cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
-                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
-                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
+                if (ldq_endian_p(be, &syscall[0]) == PK_SYS_WRITE &&
+                    ldq_endian_p(be, &syscall[1]) == HTIF_DEV_CONSOLE &&
+                    ldq_endian_p(be, &syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                     uint8_t ch;
-                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
+
+                    cpu_physical_memory_read(ldl_endian_p(be, &syscall[2]),
+                                             &ch, 1);
                     qemu_chr_fe_write(&s->chr, &ch, 1);
                     resp = 0x100 | (uint8_t)payload;
                 } else {
@@ -320,7 +323,8 @@ static const MemoryRegionOps htif_mm_ops = {
 };
 
 HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base)
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian)
 {
     uint64_t base, size, tohost_offset, fromhost_offset;
 
@@ -345,6 +349,7 @@ HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
     s->pending_read = 0;
     s->allow_tohost = 0;
     s->fromhost_inprogress = 0;
+    s->target_is_bigendian = target_is_bigendian;
     qemu_chr_fe_init(&s->chr, chr, &error_abort);
     qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
         htif_be_change, s, NULL, true);
hw/riscv/spike.c +1 -1
index 64074395bc..0989cd4a41 100644
@@ -327,7 +327,7 @@ static void spike_board_init(MachineState *machine)
 
     /* initialize HTIF using symbols found in load_kernel */
     htif_mm_init(system_memory, serial_hd(0), memmap[SPIKE_HTIF].base,
-                 htif_custom_base);
+                 htif_custom_base, TARGET_BIG_ENDIAN);
 }
 
 static void spike_set_signature(Object *obj, const char *val, Error **errp)
Signature · 1 line
2.45.2
#1 lore raw

Phil, this patch breaks 'make check-avocado' in my env:

AVOCADO tests/avocado
JOB ID : 2e98b3ea8b63d22f092ff73bdadfd975cbc27026
JOB LOG : /home/danielhb/work/qemu/build/tests/results/job-2024-10-02T10.48-2e98b3e/job.log
(01/15) tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: STARTED
(01/15) tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: INTERRUPTED: Test interrupted: Timeout reached (5.07 s)
Interrupting job (failfast).
RESULTS : PASS 0 | ERROR 0 | FAIL 0 | SKIP 14 | WARN 0 | INTERRUPT 1 | CANCEL 0
JOB TIME : 7.57 s

Test summary:
01-tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: INTERRUPTED make[1]: *** [/home/danielhb/work/qemu/tests/Makefile.include:141: check-avocado] Error 8

In fact, if you try to execute the 'spike' machine with --nographics, you're supposed to see the OpenSBI banner and boot. With this patch I don't see anything:

$ ./build/qemu-system-riscv32 -M spike --nographic
(---nothing---)

For reference this is what I applied on top of master to test it:

9de01b7f1c (HEAD -> review) hw/riscv/spike: Replace tswap64() by ldq_endian_p()
202986f968 hw/net/tulip: Use ld/st_endian_pci_dma() API
30aacb872e hw/pci/pci_device: Introduce ld/st_endian_pci_dma() API
8ee9a2310b hw/pci/pci_device: Add PCI_DMA_DEFINE_LDST_END() macro
54271f92e5 hw/virtio/virtio-access: Use ld/st_endian_phys() API
54ff063593 exec/memory_ldst_phys: Introduce ld/st_endian_phys() API
5749a411cc hw/xtensa/xtfpga: Replace memcpy()+tswap32() by stl_endian_p()
75b7a99a5d hw/xtensa/xtfpga: Remove TARGET_BIG_ENDIAN #ifdef'ry
652016da1e tests/tcg/plugins: Use the ld/st_endian_p() API
d1e4d2544a hw/mips: Add cpu_is_bigendian field to BlCpuCfg structure
6a7b3e09bb hw/mips: Pass BlCpuCfg argument to bootloader API
0466217d0e target/arm/ptw: Use the ld/st_endian_p() API
920a241f00 hw/virtio/virtio-access: Use the ld/st_endian_p() API
e5fc1a2224 qemu/bswap: Introduce ld/st_endian_p() API
062cfce8d4 (origin/master, origin/HEAD, master) Merge tag 'pull-target-arm-20241001' of https://git.linaro.org/people/pmaydell/qemu-arm into staging

Let me know if I did something wrong. Thanks,

Daniel

Quoted 102 lines · Philippe Mathieu-Daudé

Hold the target endianness in HTIFState::target_is_bigendian. Pass the target endianness as argument to htif_mm_init(). Replace tswap64() calls by ldq_endian_p() ones.

---
Based-on: <[email protected]>
"qemu/bswap: Introduce ld/st_endian_p() API"

Note: this is a non-QOM device!

---
  include/hw/char/riscv_htif.h |  4 +++-
  hw/char/riscv_htif.c         | 17 +++++++++++------
  hw/riscv/spike.c             |  2 +-
  3 files changed, 15 insertions(+), 8 deletions(-)
include/hw/char/riscv_htif.h +3 -1
index df493fdf6b..24868ddfe1 100644
@@ -35,6 +35,7 @@ typedef struct HTIFState {
      hwaddr tohost_offset;
      hwaddr fromhost_offset;
      MemoryRegion mmio;
+    bool target_is_bigendian;
 
      CharBackend chr;
      uint64_t pending_read;
@@ -49,6 +50,7 @@ void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value,
 
  /* legacy pre qom */
  HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base);
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian);
 
  #endif
hw/char/riscv_htif.c +11 -6
index 9bef60def1..77951f3c76 100644
@@ -30,7 +30,6 @@
  #include "qemu/timer.h"
  #include "qemu/error-report.h"
  #include "exec/address-spaces.h"
-#include "exec/tswap.h"
  #include "sysemu/dma.h"
  #include "sysemu/runstate.h"
 
@@ -211,13 +210,17 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
                      SHUTDOWN_CAUSE_GUEST_SHUTDOWN, exit_code);
                  return;
              } else {
+                bool be = s->target_is_bigendian;
                  uint64_t syscall[8];
+
                  cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
-                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
-                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
+                if (ldq_endian_p(be, &syscall[0]) == PK_SYS_WRITE &&
+                    ldq_endian_p(be, &syscall[1]) == HTIF_DEV_CONSOLE &&
+                    ldq_endian_p(be, &syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                      uint8_t ch;
-                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
+
+                    cpu_physical_memory_read(ldl_endian_p(be, &syscall[2]),
+                                             &ch, 1);
                      qemu_chr_fe_write(&s->chr, &ch, 1);
                      resp = 0x100 | (uint8_t)payload;
                  } else {
@@ -320,7 +323,8 @@ static const MemoryRegionOps htif_mm_ops = {
  };
 
  HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base)
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian)
  {
      uint64_t base, size, tohost_offset, fromhost_offset;
 
@@ -345,6 +349,7 @@ HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
      s->pending_read = 0;
      s->allow_tohost = 0;
      s->fromhost_inprogress = 0;
+    s->target_is_bigendian = target_is_bigendian;
      qemu_chr_fe_init(&s->chr, chr, &error_abort);
      qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
          htif_be_change, s, NULL, true);
hw/riscv/spike.c +1 -1
index 64074395bc..0989cd4a41 100644
@@ -327,7 +327,7 @@ static void spike_board_init(MachineState *machine)
 
      /* initialize HTIF using symbols found in load_kernel */
      htif_mm_init(system_memory, serial_hd(0), memmap[SPIKE_HTIF].base,
-                 htif_custom_base);
+                 htif_custom_base, TARGET_BIG_ENDIAN);
  }
 
  static void spike_set_signature(Object *obj, const char *val, Error **errp)
#2 lore raw
Quoted 110 lines · Daniel Henrique Barboza

Phil, this patch breaks 'make check-avocado' in my env:

  AVOCADO tests/avocado
JOB ID     : 2e98b3ea8b63d22f092ff73bdadfd975cbc27026
JOB LOG    : /home/danielhb/work/qemu/build/tests/results/job-2024-10-02T10.48-2e98b3e/job.log
 (01/15) tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: STARTED (01/15) tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike:
INTERRUPTED: Test interrupted: Timeout reached (5.07 s)
Interrupting job (failfast).
RESULTS    : PASS 0 | ERROR 0 | FAIL 0 | SKIP 14 | WARN 0 | INTERRUPT 1 | CANCEL 0 JOB TIME   : 7.57 s

Test summary:
01-tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: INTERRUPTED make[1]: *** [/home/danielhb/work/qemu/tests/Makefile.include:141: check-avocado] Error 8

In fact, if you try to execute the 'spike' machine with --nographics, you're supposed to see the OpenSBI banner and boot. With this patch I don't see anything:

$ ./build/qemu-system-riscv32 -M spike --nographic
(---nothing---)

For reference this is what I applied on top of master to test it:

9de01b7f1c (HEAD -> review) hw/riscv/spike: Replace tswap64() by ldq_endian_p()
202986f968 hw/net/tulip: Use ld/st_endian_pci_dma() API
30aacb872e hw/pci/pci_device: Introduce ld/st_endian_pci_dma() API
8ee9a2310b hw/pci/pci_device: Add PCI_DMA_DEFINE_LDST_END() macro
54271f92e5 hw/virtio/virtio-access: Use ld/st_endian_phys() API
54ff063593 exec/memory_ldst_phys: Introduce ld/st_endian_phys() API
5749a411cc hw/xtensa/xtfpga: Replace memcpy()+tswap32() by stl_endian_p()
75b7a99a5d hw/xtensa/xtfpga: Remove TARGET_BIG_ENDIAN #ifdef'ry
652016da1e tests/tcg/plugins: Use the ld/st_endian_p() API
d1e4d2544a hw/mips: Add cpu_is_bigendian field to BlCpuCfg structure
6a7b3e09bb hw/mips: Pass BlCpuCfg argument to bootloader API
0466217d0e target/arm/ptw: Use the ld/st_endian_p() API
920a241f00 hw/virtio/virtio-access: Use the ld/st_endian_p() API
e5fc1a2224 qemu/bswap: Introduce ld/st_endian_p() API
062cfce8d4 (origin/master, origin/HEAD, master) Merge tag 'pull-target-arm-20241001' of https://git.linaro.org/people/pmaydell/qemu-arm into staging

Let me know if I did something wrong. Thanks,

Daniel

Quoted 57 lines · Philippe Mathieu-Daudé

Hold the target endianness in HTIFState::target_is_bigendian. Pass the target endianness as argument to htif_mm_init(). Replace tswap64() calls by ldq_endian_p() ones.

---
Based-on: <[email protected]> "qemu/bswap: Introduce ld/st_endian_p() API"

Note: this is a non-QOM device!
---
  include/hw/char/riscv_htif.h |  4 +++- hw/char/riscv_htif.c         | 17 +++++++++++------ hw/riscv/spike.c             |  2 +-

  3 files changed, 15 insertions(+), 8 deletions(-)
include/hw/char/riscv_htif.h +3 -1
index df493fdf6b..24868ddfe1 100644
@@ -35,6 +35,7 @@ typedef struct HTIFState {
       hwaddr tohost_offset;
       hwaddr fromhost_offset;
       MemoryRegion mmio;
+    bool target_is_bigendian;
       CharBackend chr;
       uint64_t pending_read;
@@ -49,6 +50,7 @@ void htif_symbol_callback(const char *st_name, int st_info, 
 uint64_t st_value,
   /* legacy pre qom */
   HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base);
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian);
   #endif
hw/char/riscv_htif.c +2 -3
index 9bef60def1..77951f3c76 100644
@@ -30,7 +30,6 @@
   #include "qemu/timer.h"
   #include "qemu/error-report.h"
   #include "exec/address-spaces.h"
-#include "exec/tswap.h"
   #include "sysemu/dma.h"
   #include "sysemu/runstate.h"
@@ -211,13 +210,17 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t 
 val_written)
                       SHUTDOWN_CAUSE_GUEST_SHUTDOWN, exit_code);
                   return;
               } else {
+                bool be = s->target_is_bigendian;
                   uint64_t syscall[8];
+
                   cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
-                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
Quoted 8 lines

hw/char/riscv_htif.c static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written) · line 222

Quoted 8 lines
hw/char/riscv_htif.c +5 -2
-                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
+                if (ldq_endian_p(be, &syscall[0]) == PK_SYS_WRITE &&
+                    ldq_endian_p(be, &syscall[1]) == HTIF_DEV_CONSOLE &&
+                    ldq_endian_p(be, &syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                       uint8_t ch;
-                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
+
+                    cpu_physical_memory_read(ldl_endian_p(be, &syscall[2]),
                                                 ^^^^^^^^^^^^

Shouldn't this be ldq_endian_p() for a 64-bit value?

Quoted 27 lines
Quoted 27 lines
hw/char/riscv_htif.c +1 -0
+                                             &ch, 1);
 
 

                      qemu_chr_fe_write(&s->chr, &ch, 1); resp = 0x100 | (uint8_t)payload; } else {

hw/char/riscv_htif.c +3 -1
@@ -320,7 +323,8 @@ static const MemoryRegionOps htif_mm_ops = {
   };
   HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base)
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian)
   {
       uint64_t base, size, tohost_offset, fromhost_offset;
@@ -345,6 +349,7 @@ HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
       s->pending_read = 0;
       s->allow_tohost = 0;
       s->fromhost_inprogress = 0;
+    s->target_is_bigendian = target_is_bigendian;
       qemu_chr_fe_init(&s->chr, chr, &error_abort);
       qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
           htif_be_change, s, NULL, true);
hw/riscv/spike.c +0 -1
index 64074395bc..0989cd4a41 100644
--- a/hw/riscv/spike.c
Quoted 8 lines

hw/riscv/spike.c static void spike_board_init(MachineState *machine) · line 331

Quoted 8 lines

+++ b/hw/riscv/spike.c

hw/riscv/spike.c +1 -1
@@ -327,7 +327,7 @@ static void spike_board_init(MachineState *machine)
       /* initialize HTIF using symbols found in load_kernel */
       htif_mm_init(system_memory, serial_hd(0), memmap[SPIKE_HTIF].base,
-                 htif_custom_base);
+                 htif_custom_base, TARGET_BIG_ENDIAN);
   }
   static void spike_set_signature(Object *obj, const char *val, Error **errp)

ATB,

Mark.

#3 lore raw
Quoted 120 lines · Mark Cave-Ayland
Quoted 107 lines · Daniel Henrique Barboza

Phil, this patch breaks 'make check-avocado' in my env:

   AVOCADO tests/avocado
JOB ID     : 2e98b3ea8b63d22f092ff73bdadfd975cbc27026
JOB LOG    : /home/danielhb/work/qemu/build/tests/results/job-2024-10-02T10.48-2e98b3e/job.log
  (01/15) tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: STARTED
  (01/15) tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: INTERRUPTED: Test interrupted: Timeout reached (5.07 s) Interrupting job (failfast).
RESULTS    : PASS 0 | ERROR 0 | FAIL 0 | SKIP 14 | WARN 0 | INTERRUPT 1 | CANCEL 0
JOB TIME   : 7.57 s

Test summary:
01-tests/avocado/riscv_opensbi.py:RiscvOpenSBI.test_riscv32_spike: INTERRUPTED make[1]: *** [/home/danielhb/work/qemu/tests/Makefile.include:141: check-avocado] Error 8

In fact, if you try to execute the 'spike' machine with --nographics, you're supposed to see the OpenSBI banner and boot. With this patch I don't see anything:

$ ./build/qemu-system-riscv32 -M spike --nographic
(---nothing---)

For reference this is what I applied on top of master to test it:

9de01b7f1c (HEAD -> review) hw/riscv/spike: Replace tswap64() by ldq_endian_p()
202986f968 hw/net/tulip: Use ld/st_endian_pci_dma() API
30aacb872e hw/pci/pci_device: Introduce ld/st_endian_pci_dma() API
8ee9a2310b hw/pci/pci_device: Add PCI_DMA_DEFINE_LDST_END() macro
54271f92e5 hw/virtio/virtio-access: Use ld/st_endian_phys() API
54ff063593 exec/memory_ldst_phys: Introduce ld/st_endian_phys() API
5749a411cc hw/xtensa/xtfpga: Replace memcpy()+tswap32() by stl_endian_p()
75b7a99a5d hw/xtensa/xtfpga: Remove TARGET_BIG_ENDIAN #ifdef'ry
652016da1e tests/tcg/plugins: Use the ld/st_endian_p() API
d1e4d2544a hw/mips: Add cpu_is_bigendian field to BlCpuCfg structure
6a7b3e09bb hw/mips: Pass BlCpuCfg argument to bootloader API
0466217d0e target/arm/ptw: Use the ld/st_endian_p() API
920a241f00 hw/virtio/virtio-access: Use the ld/st_endian_p() API
e5fc1a2224 qemu/bswap: Introduce ld/st_endian_p() API
062cfce8d4 (origin/master, origin/HEAD, master) Merge tag 'pull-target-arm-20241001' of https://git.linaro.org/people/pmaydell/qemu-arm into staging

Let me know if I did something wrong. Thanks,

Daniel

Quoted 55 lines · Philippe Mathieu-Daudé

Hold the target endianness in HTIFState::target_is_bigendian. Pass the target endianness as argument to htif_mm_init(). Replace tswap64() calls by ldq_endian_p() ones.

---
Based-on: <[email protected]> "qemu/bswap: Introduce ld/st_endian_p() API"

Note: this is a non-QOM device!
---
  include/hw/char/riscv_htif.h |  4 +++- hw/char/riscv_htif.c         | 17 +++++++++++------ hw/riscv/spike.c             |  2 +-

  3 files changed, 15 insertions(+), 8 deletions(-)
include/hw/char/riscv_htif.h +3 -1
index df493fdf6b..24868ddfe1 100644
@@ -35,6 +35,7 @@ typedef struct HTIFState {
       hwaddr tohost_offset;
       hwaddr fromhost_offset;
       MemoryRegion mmio;
+    bool target_is_bigendian;
       CharBackend chr;
       uint64_t pending_read;
@@ -49,6 +50,7 @@ void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value,
   /* legacy pre qom */
   HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base);
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian);
   #endif
hw/char/riscv_htif.c +2 -3
index 9bef60def1..77951f3c76 100644
@@ -30,7 +30,6 @@
   #include "qemu/timer.h"
   #include "qemu/error-report.h"
   #include "exec/address-spaces.h"
-#include "exec/tswap.h"
   #include "sysemu/dma.h"
   #include "sysemu/runstate.h"
@@ -211,13 +210,17 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
                       SHUTDOWN_CAUSE_GUEST_SHUTDOWN, exit_code);
                   return;
               } else {
+                bool be = s->target_is_bigendian;
                   uint64_t syscall[8];
+
                   cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
-                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
Quoted 8 lines

hw/char/riscv_htif.c static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written) · line 222

Quoted 8 lines
hw/char/riscv_htif.c +5 -2
-                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
+                if (ldq_endian_p(be, &syscall[0]) == PK_SYS_WRITE &&
+                    ldq_endian_p(be, &syscall[1]) == HTIF_DEV_CONSOLE &&
+                    ldq_endian_p(be, &syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                       uint8_t ch;
-                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
+
+                    cpu_physical_memory_read(ldl_endian_p(be, &syscall[2]),

                                                 ^^^^^^^^^^^^

Quoted 1 line

Shouldn't this be ldq_endian_p() for a 64-bit value?

Bingo! This change fixes make check-avocado and the OpenSBI boot:

$ git diff
hw/char/riscv_htif.c +1 -1
index 77951f3c76..0ed038a70c 100644
@@ -219,7 +219,7 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
                     ldq_endian_p(be, &syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                     uint8_t ch;
 
-                    cpu_physical_memory_read(ldl_endian_p(be, &syscall[2]),
+                    cpu_physical_memory_read(ldq_endian_p(be, &syscall[2]),
                                              &ch, 1);
                     qemu_chr_fe_write(&s->chr, &ch, 1);
                     resp = 0x100 | (uint8_t)payload;
$ ./build/qemu-system-riscv32 -M spike --nographic

OpenSBI v1.5.1
____ _____ ____ _____

  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

(...)

Thanks,

Daniel

Quoted 42 lines
Quoted 28 lines
Quoted 28 lines
hw/char/riscv_htif.c +1 -0
+                                             &ch, 1);
 
 
 

                      qemu_chr_fe_write(&s->chr, &ch, 1); resp = 0x100 | (uint8_t)payload; } else {

hw/char/riscv_htif.c +3 -1
@@ -320,7 +323,8 @@ static const MemoryRegionOps htif_mm_ops = {
   };
   HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base)
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian)
   {
       uint64_t base, size, tohost_offset, fromhost_offset;
@@ -345,6 +349,7 @@ HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
       s->pending_read = 0;
       s->allow_tohost = 0;
       s->fromhost_inprogress = 0;
+    s->target_is_bigendian = target_is_bigendian;
       qemu_chr_fe_init(&s->chr, chr, &error_abort);
       qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
           htif_be_change, s, NULL, true);
hw/riscv/spike.c +0 -1
index 64074395bc..0989cd4a41 100644
--- a/hw/riscv/spike.c
Quoted 8 lines

hw/riscv/spike.c static void spike_board_init(MachineState *machine) · line 331

Quoted 8 lines

+++ b/hw/riscv/spike.c

hw/riscv/spike.c +1 -1
@@ -327,7 +327,7 @@ static void spike_board_init(MachineState *machine)
       /* initialize HTIF using symbols found in load_kernel */
       htif_mm_init(system_memory, serial_hd(0), memmap[SPIKE_HTIF].base,
-                 htif_custom_base);
+                 htif_custom_base, TARGET_BIG_ENDIAN);
   }
   static void spike_set_signature(Object *obj, const char *val, Error **errp)

ATB,

Mark.

#4 lore raw
Quoted 79 lines · Daniel Henrique Barboza
Quoted 78 lines · Mark Cave-Ayland
Quoted 65 lines · Daniel Henrique Barboza

Phil, this patch breaks 'make check-avocado' in my env:

Quoted 61 lines · Philippe Mathieu-Daudé

Hold the target endianness in HTIFState::target_is_bigendian. Pass the target endianness as argument to htif_mm_init(). Replace tswap64() calls by ldq_endian_p() ones.

---
Based-on: <[email protected]> "qemu/bswap: Introduce ld/st_endian_p() API"

Note: this is a non-QOM device!
---
  include/hw/char/riscv_htif.h |  4 +++- hw/char/riscv_htif.c         | 17 +++++++++++------ hw/riscv/spike.c             |  2 +-

  3 files changed, 15 insertions(+), 8 deletions(-)
include/hw/char/riscv_htif.h +3 -1
index df493fdf6b..24868ddfe1 100644
@@ -35,6 +35,7 @@ typedef struct HTIFState {
       hwaddr tohost_offset;
       hwaddr fromhost_offset;
       MemoryRegion mmio;
+    bool target_is_bigendian;
       CharBackend chr;
       uint64_t pending_read;
@@ -49,6 +50,7 @@ void htif_symbol_callback(const char *st_name, int 
 st_info, uint64_t st_value,
   /* legacy pre qom */
   HTIFState *htif_mm_init(MemoryRegion *address_space, Chardev *chr,
-                        uint64_t nonelf_base, bool custom_base);
+                        uint64_t nonelf_base, bool custom_base,
+                        bool target_is_bigendian);
   #endif
hw/char/riscv_htif.c +4 -4
index 9bef60def1..77951f3c76 100644
@@ -30,7 +30,6 @@
   #include "qemu/timer.h"
   #include "qemu/error-report.h"
   #include "exec/address-spaces.h"
-#include "exec/tswap.h"
   #include "sysemu/dma.h"
   #include "sysemu/runstate.h"
@@ -211,13 +210,17 @@ static void htif_handle_tohost_write(HTIFState 
 *s, uint64_t val_written)
                       SHUTDOWN_CAUSE_GUEST_SHUTDOWN, exit_code);
                   return;
               } else {
+                bool be = s->target_is_bigendian;
                   uint64_t syscall[8];
+
                   cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
-                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
-                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
+                if (ldq_endian_p(be, &syscall[0]) == PK_SYS_WRITE &&
+                    ldq_endian_p(be, &syscall[1]) == 

HTIF_DEV_CONSOLE &&

Quoted 8 lines
Quoted 8 lines

+                    ldq_endian_p(be, &syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                      uint8_t ch;
-                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
+
+                    cpu_physical_memory_read(ldl_endian_p(be, &syscall[2]),

                                                  ^^^^^^^^^^^^

Quoted 1 line
Quoted 1 line

Shouldn't this be ldq_endian_p() for a 64-bit value?

Oops, thanks Mark, stupid c/p mistake.

Quoted 26 lines

Bingo! This change fixes make check-avocado and the OpenSBI boot:

$ git diff
hw/char/riscv_htif.c +1 -1
index 77951f3c76..0ed038a70c 100644
@@ -219,7 +219,7 @@ static void htif_handle_tohost_write(HTIFState *s, 
 uint64_t val_written)
                      ldq_endian_p(be, &syscall[3]) == 
 HTIF_CONSOLE_CMD_PUTC) {
                      uint8_t ch;
 
-                    cpu_physical_memory_read(ldl_endian_p(be, 
 &syscall[2]),
+                    cpu_physical_memory_read(ldq_endian_p(be, 

&syscall[2]),
                                              &ch, 1); qemu_chr_fe_write(&s->chr, &ch, 1); resp = 0x100 | (uint8_t)payload;

$ ./build/qemu-system-riscv32 -M spike --nographic

OpenSBI v1.5.1
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|

Quoted 7 lines
  | |  | |_ __   ___ _ __ | (___ | |_) || |
  | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
  | |__| | |_) |  __/ | | |____) | |_) || |_
   \____/| .__/ \___|_| |_|_____/|____/_____|
         | |
         |_|

(...)

I'll take that as a T-b tag :P Thanks Daniel!