Skip to contentSkip to conversation

riscv: Fix the console of the Spike machine on big endian hosts

PATCH Applied Thomas Huth 5 participants 11 messages Jul 21, 2023 – Jul 23, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

cover riscv: Fix the console of the Spike machine on big endian hosts

The tests/avocado/riscv_opensbi.py avocado test is currently failing on big endian hosts since the console of the Spike machine is not working there. With two small patches, this can be fixed: First patch fixes riscv64, and the second one fixes riscv32.

Thomas Huth (2):
hw/char/riscv_htif: Fix printing of console characters on big endian
hosts
hw/char/riscv_htif: Fix the console syscall on big endian hosts

 hw/char/riscv_htif.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.39.3
#1 lore raw

PATCH 1/2 hw/char/riscv_htif: Fix printing of console characters on big endian hosts

The character that should be printed is stored in the 64 bit "payload" variable. The code currently tries to print it by taking the address of the variable and passing this pointer to qemu_chr_fe_write(). However, this only works on little endian hosts where the least significant bits are stored on the lowest address. To do this in a portable way, we have to store the value in an uint8_t variable instead.

Fixes 5033606780 ("RISC-V HTIF Console")
---
 hw/char/riscv_htif.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/char/riscv_htif.c +2 -1
index 37d3ccc76b..f96df40124 100644
@@ -232,7 +232,8 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
             s->tohost = 0; /* clear to indicate we read */
             return;
         } else if (cmd == HTIF_CONSOLE_CMD_PUTC) {
-            qemu_chr_fe_write(&s->chr, (uint8_t *)&payload, 1);
+            uint8_t ch = (uint8_t)payload;
+            qemu_chr_fe_write(&s->chr, &ch, 1);
             resp = 0x100 | (uint8_t)payload;
         } else {
             qemu_log("HTIF device %d: unknown command\n", device);
Signature · 1 line
2.39.3
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/char/riscv_htif: Fix printing of console characters on big endian hosts

Quoted 8 lines · Thomas Huth

The character that should be printed is stored in the 64 bit "payload" variable. The code currently tries to print it by taking the address of the variable and passing this pointer to qemu_chr_fe_write(). However, this only works on little endian hosts where the least significant bits are stored on the lowest address. To do this in a portable way, we have to store the value in an uint8_t variable instead.

Quoted 6 lines
Fixes 5033606780 ("RISC-V HTIF Console")
---
 hw/char/riscv_htif.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] hw/char/riscv_htif: Fix printing of console characters on big endian hosts

In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

The character that should be printed is stored in the 64 bit "payload" variable. The code currently tries to print it by taking the address of the variable and passing this pointer to qemu_chr_fe_write(). However, this only works on little endian hosts where the least significant bits are stored on the lowest address. To do this in a portable way, we have to store the value in an uint8_t variable instead.

Fixes 5033606780 ("RISC-V HTIF Console")

---

Quoted 17 lines
  hw/char/riscv_htif.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/char/riscv_htif.c +2 -1
index 37d3ccc76b..f96df40124 100644
@@ -232,7 +232,8 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
              s->tohost = 0; /* clear to indicate we read */
              return;
          } else if (cmd == HTIF_CONSOLE_CMD_PUTC) {
-            qemu_chr_fe_write(&s->chr, (uint8_t *)&payload, 1);
+            uint8_t ch = (uint8_t)payload;
+            qemu_chr_fe_write(&s->chr, &ch, 1);
              resp = 0x100 | (uint8_t)payload;
          } else {
              qemu_log("HTIF device %d: unknown command\n", device);
Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] hw/char/riscv_htif: Fix printing of console characters on big endian hosts

In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

The character that should be printed is stored in the 64 bit "payload" variable. The code currently tries to print it by taking the address of the variable and passing this pointer to qemu_chr_fe_write(). However, this only works on little endian hosts where the least significant bits are stored on the lowest address. To do this in a portable way, we have to store the value in an uint8_t variable instead.

Quoted 5 lines
Fixes 5033606780 ("RISC-V HTIF Console")
---
  hw/char/riscv_htif.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
Reviewed-by on v1 1/2
#5 lore raw

Re: [PATCH 1/2] hw/char/riscv_htif: Fix printing of console characters on big endian hosts

In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

The character that should be printed is stored in the 64 bit "payload" variable. The code currently tries to print it by taking the address of the variable and passing this pointer to qemu_chr_fe_write(). However, this only works on little endian hosts where the least significant bits are stored on the lowest address. To do this in a portable way, we have to store the value in an uint8_t variable instead.

Fixes 5033606780 ("RISC-V HTIF Console")

Alistair

Quoted 22 lines
---
 hw/char/riscv_htif.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/char/riscv_htif.c +2 -2
index 37d3ccc76b..f96df40124 100644
@@ -232,7 +232,8 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
             s->tohost = 0; /* clear to indicate we read */
             return;
         } else if (cmd == HTIF_CONSOLE_CMD_PUTC) {
-            qemu_chr_fe_write(&s->chr, (uint8_t *)&payload, 1);
+            uint8_t ch = (uint8_t)payload;
+            qemu_chr_fe_write(&s->chr, &ch, 1);
             resp = 0x100 | (uint8_t)payload;
         } else {
             qemu_log("HTIF device %d: unknown command\n", device);
--

2.39.3

#6 lore raw

PATCH 2/2 hw/char/riscv_htif: Fix the console syscall on big endian hosts

In reply to Thomas Huth

Values that have been read via cpu_physical_memory_read() from the guest's memory have to be swapped in case the host endianess differs from the guest.

Fixes a6e13e31d5 ("riscv_htif: Support console output via proxy syscall")
---
 hw/char/riscv_htif.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/char/riscv_htif.c +5 -4
index f96df40124..40de6b8b77 100644
@@ -30,6 +30,7 @@
 #include "qemu/timer.h"
 #include "qemu/error-report.h"
 #include "exec/address-spaces.h"
+#include "exec/tswap.h"
 #include "sysemu/dma.h"
 
 #define RISCV_DEBUG_HTIF 0
@@ -209,11 +210,11 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
             } else {
                 uint64_t syscall[8];
                 cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (syscall[0] == PK_SYS_WRITE &&
-                    syscall[1] == HTIF_DEV_CONSOLE &&
-                    syscall[3] == HTIF_CONSOLE_CMD_PUTC) {
+                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
+                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
+                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                     uint8_t ch;
-                    cpu_physical_memory_read(syscall[2], &ch, 1);
+                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
                     qemu_chr_fe_write(&s->chr, &ch, 1);
                     resp = 0x100 | (uint8_t)payload;
                 } else {
Signature · 1 line
2.39.3
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] hw/char/riscv_htif: Fix the console syscall on big endian hosts

Quoted 3 lines · Thomas Huth

Values that have been read via cpu_physical_memory_read() from the guest's memory have to be swapped in case the host endianess differs

typo: endianness

Quoted 8 lines

from the guest.

Fixes a6e13e31d5 ("riscv_htif: Support console output via proxy syscall")
---
 hw/char/riscv_htif.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] hw/char/riscv_htif: Fix the console syscall on big endian hosts

In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

Values that have been read via cpu_physical_memory_read() from the guest's memory have to be swapped in case the host endianess differs from the guest.

Fixes a6e13e31d5 ("riscv_htif: Support console output via proxy syscall")

---

Quoted 31 lines
  hw/char/riscv_htif.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
hw/char/riscv_htif.c +5 -4
index f96df40124..40de6b8b77 100644
@@ -30,6 +30,7 @@
  #include "qemu/timer.h"
  #include "qemu/error-report.h"
  #include "exec/address-spaces.h"
+#include "exec/tswap.h"
  #include "sysemu/dma.h"
 
  #define RISCV_DEBUG_HTIF 0
@@ -209,11 +210,11 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
              } else {
                  uint64_t syscall[8];
                  cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (syscall[0] == PK_SYS_WRITE &&
-                    syscall[1] == HTIF_DEV_CONSOLE &&
-                    syscall[3] == HTIF_CONSOLE_CMD_PUTC) {
+                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
+                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
+                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                      uint8_t ch;
-                    cpu_physical_memory_read(syscall[2], &ch, 1);
+                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
                      qemu_chr_fe_write(&s->chr, &ch, 1);
                      resp = 0x100 | (uint8_t)payload;
                  } else {
Reviewed-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] hw/char/riscv_htif: Fix the console syscall on big endian hosts

In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

Values that have been read via cpu_physical_memory_read() from the guest's memory have to be swapped in case the host endianess differs from the guest.

Fixes a6e13e31d5 ("riscv_htif: Support console output via proxy syscall")

Alistair

Quoted 36 lines
---
 hw/char/riscv_htif.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/char/riscv_htif.c +5 -5
index f96df40124..40de6b8b77 100644
@@ -30,6 +30,7 @@
 #include "qemu/timer.h"
 #include "qemu/error-report.h"
 #include "exec/address-spaces.h"
+#include "exec/tswap.h"
 #include "sysemu/dma.h"
 
 #define RISCV_DEBUG_HTIF 0
@@ -209,11 +210,11 @@ static void htif_handle_tohost_write(HTIFState *s, uint64_t val_written)
             } else {
                 uint64_t syscall[8];
                 cpu_physical_memory_read(payload, syscall, sizeof(syscall));
-                if (syscall[0] == PK_SYS_WRITE &&
-                    syscall[1] == HTIF_DEV_CONSOLE &&
-                    syscall[3] == HTIF_CONSOLE_CMD_PUTC) {
+                if (tswap64(syscall[0]) == PK_SYS_WRITE &&
+                    tswap64(syscall[1]) == HTIF_DEV_CONSOLE &&
+                    tswap64(syscall[3]) == HTIF_CONSOLE_CMD_PUTC) {
                     uint8_t ch;
-                    cpu_physical_memory_read(syscall[2], &ch, 1);
+                    cpu_physical_memory_read(tswap64(syscall[2]), &ch, 1);
                     qemu_chr_fe_write(&s->chr, &ch, 1);
                     resp = 0x100 | (uint8_t)payload;
                 } else {
--

2.39.3

#10 lore raw
In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

The tests/avocado/riscv_opensbi.py avocado test is currently failing on big endian hosts since the console of the Spike machine is not working there. With two small patches, this can be fixed: First patch fixes riscv64, and the second one fixes riscv32.

Thomas Huth (2):
hw/char/riscv_htif: Fix printing of console characters on big endian
hosts
hw/char/riscv_htif: Fix the console syscall on big endian hosts

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 8 lines
 hw/char/riscv_htif.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

--
2.39.3