[PATCH v3 3/3] semihosting/uaccess: Use the cpu_internal_tswap() functions

Martin Kröning via qemu development posted 3 patches 3 weeks, 6 days ago
Maintainers: Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, "Michael S. Tsirkin" <mst@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Peter Maydell <peter.maydell@linaro.org>, Nicholas Piggin <npiggin@gmail.com>, Chinmay Rath <rathc@linux.ibm.com>, Glenn Miles <milesg@linux.ibm.com>
[PATCH v3 3/3] semihosting/uaccess: Use the cpu_internal_tswap() functions
Posted by Martin Kröning via qemu development 3 weeks, 6 days ago
The semihosting ABI [1] states:
> Multi-byte values in memory must be formatted as pure little-endian or pure
> big-endian to match the endianness mapping configuration of the processor.

This commits ensures that semihosting data is properly byte-swapped if
the guest's CPU is currently in a different runtime-configurable
endianness than the host's CPU.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3258
Buglink: https://github.com/taiki-e/semihosting/issues/18

[1]: https://github.com/ARM-software/abi-aa/blob/2025Q1/semihosting/semihosting.rst#the-semihosting-interface

Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
---
 include/semihosting/uaccess.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/semihosting/uaccess.h b/include/semihosting/uaccess.h
index 2093a49827..0911e3b31d 100644
--- a/include/semihosting/uaccess.h
+++ b/include/semihosting/uaccess.h
@@ -28,7 +28,7 @@
     ({ uint64_t val_ = 0;                                               \
        int ret_ = cpu_memory_rw_debug(env_cpu(env), (addr),             \
                                       &val_, sizeof(val_), 0);          \
-       (val) = tswap64(val_); ret_; })
+       (val) = cpu_internal_tswap64(cs, val_); ret_; })
 
 /**
  * get_user_u32:
@@ -39,7 +39,7 @@
     ({ uint32_t val_ = 0;                                               \
        int ret_ = cpu_memory_rw_debug(env_cpu(env), (addr),             \
                                       &val_, sizeof(val_), 0);          \
-       (val) = tswap32(val_); ret_; })
+       (val) = cpu_internal_tswap32(cs, val_); ret_; })
 
 /**
  * get_user_u8:
@@ -65,7 +65,7 @@
  * Returns: 0 on success, -1 on error.
  */
 #define put_user_u64(val, addr)                                         \
-    ({ uint64_t val_ = tswap64(val);                                    \
+    ({ uint64_t val_ = cpu_internal_tswap64(cs, val);                   \
        cpu_memory_rw_debug(env_cpu(env), (addr), &val_, sizeof(val_), 1); })
 
 /**
@@ -74,7 +74,7 @@
  * Returns: 0 on success, -1 on error.
  */
 #define put_user_u32(val, addr)                                         \
-    ({ uint32_t val_ = tswap32(val);                                    \
+    ({ uint32_t val_ = cpu_internal_tswap32(cs, val);                   \
        cpu_memory_rw_debug(env_cpu(env), (addr), &val_, sizeof(val_), 1); })
 
 /**

-- 
Git-155)


Re: [PATCH v3 3/3] semihosting/uaccess: Use the cpu_internal_tswap() functions
Posted by Philippe Mathieu-Daudé 3 weeks, 5 days ago
On 11/3/26 17:27, Martin Kröning wrote:
> The semihosting ABI [1] states:
>> Multi-byte values in memory must be formatted as pure little-endian or pure
>> big-endian to match the endianness mapping configuration of the processor.
> 
> This commits ensures that semihosting data is properly byte-swapped if
> the guest's CPU is currently in a different runtime-configurable
> endianness than the host's CPU.

This patch also remove the target-specificity in semihosting,
which is an excellent news for me \o/

> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3258
> Buglink: https://github.com/taiki-e/semihosting/issues/18
> 
> [1]: https://github.com/ARM-software/abi-aa/blob/2025Q1/semihosting/semihosting.rst#the-semihosting-interface
> 
> Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
> Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>   include/semihosting/uaccess.h | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/include/semihosting/uaccess.h b/include/semihosting/uaccess.h
> index 2093a49827..0911e3b31d 100644
> --- a/include/semihosting/uaccess.h
> +++ b/include/semihosting/uaccess.h
> @@ -28,7 +28,7 @@
>       ({ uint64_t val_ = 0;                                               \
>          int ret_ = cpu_memory_rw_debug(env_cpu(env), (addr),             \
>                                         &val_, sizeof(val_), 0);          \
> -       (val) = tswap64(val_); ret_; })
> +       (val) = cpu_internal_tswap64(cs, val_); ret_; })
>   
>   /**
>    * get_user_u32:
> @@ -39,7 +39,7 @@
>       ({ uint32_t val_ = 0;                                               \
>          int ret_ = cpu_memory_rw_debug(env_cpu(env), (addr),             \
>                                         &val_, sizeof(val_), 0);          \
> -       (val) = tswap32(val_); ret_; })
> +       (val) = cpu_internal_tswap32(cs, val_); ret_; })
>   
>   /**
>    * get_user_u8:
> @@ -65,7 +65,7 @@
>    * Returns: 0 on success, -1 on error.
>    */
>   #define put_user_u64(val, addr)                                         \
> -    ({ uint64_t val_ = tswap64(val);                                    \
> +    ({ uint64_t val_ = cpu_internal_tswap64(cs, val);                   \
>          cpu_memory_rw_debug(env_cpu(env), (addr), &val_, sizeof(val_), 1); })
>   
>   /**
> @@ -74,7 +74,7 @@
>    * Returns: 0 on success, -1 on error.
>    */
>   #define put_user_u32(val, addr)                                         \
> -    ({ uint32_t val_ = tswap32(val);                                    \
> +    ({ uint32_t val_ = cpu_internal_tswap32(cs, val);                   \
>          cpu_memory_rw_debug(env_cpu(env), (addr), &val_, sizeof(val_), 1); })
>   
>   /**
>