diff mbox series

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

Message ID 20230721094720.902454-3-thuth@redhat.com
State New
Headers show
Series riscv: Fix the console of the Spike machine on big endian hosts | expand

Commit Message

Thomas Huth July 21, 2023, 9:47 a.m. UTC
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")
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 hw/char/riscv_htif.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Bin Meng July 21, 2023, 10:10 a.m. UTC | #1
On Fri, Jul 21, 2023 at 5:48 PM Thomas Huth <thuth@redhat.com> wrote:
>
> 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

> from the guest.
>
> Fixes: a6e13e31d5 ("riscv_htif: Support console output via proxy syscall")
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  hw/char/riscv_htif.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>

Reviewed-by: Bin Meng <bmeng@tinylab.org>
Daniel Henrique Barboza July 21, 2023, 10:20 a.m. UTC | #2
On 7/21/23 06:47, Thomas Huth wrote:
> 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")
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---

Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>

>   hw/char/riscv_htif.c | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/char/riscv_htif.c b/hw/char/riscv_htif.c
> index f96df40124..40de6b8b77 100644
> --- a/hw/char/riscv_htif.c
> +++ b/hw/char/riscv_htif.c
> @@ -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 {
Alistair Francis July 23, 2023, 10:37 p.m. UTC | #3
On Fri, Jul 21, 2023 at 7:48 PM Thomas Huth <thuth@redhat.com> wrote:
>
> 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")
> Signed-off-by: Thomas Huth <thuth@redhat.com>

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

> ---
>  hw/char/riscv_htif.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/hw/char/riscv_htif.c b/hw/char/riscv_htif.c
> index f96df40124..40de6b8b77 100644
> --- a/hw/char/riscv_htif.c
> +++ b/hw/char/riscv_htif.c
> @@ -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
>
>
diff mbox series

Patch

diff --git a/hw/char/riscv_htif.c b/hw/char/riscv_htif.c
index f96df40124..40de6b8b77 100644
--- a/hw/char/riscv_htif.c
+++ b/hw/char/riscv_htif.c
@@ -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 {