From: "Cédric Le Goater" <clg@kaod.org>
To: "Thomas Huth" <thuth@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>,
qemu-devel@nongnu.org,
"Philippe Mathieu-Daudé" <philmd@linaro.org>
Cc: <qemu-ppc@nongnu.org>, Paolo Bonzini <pbonzini@redhat.com>,
Richard Henderson <richard.henderson@linaro.org>,
David Gibson <david@gibson.dropbear.id.au>,
Daniel Henrique Barboza <danielhb413@gmail.com>
Subject: Re: [PATCH 2/3] include/exec: Provide the tswap() functions for target independent code, too
Date: Wed, 12 Apr 2023 13:01:35 +0200 [thread overview]
Message-ID: <b4f80d34-886c-6fb5-587f-728cde13f57d@kaod.org> (raw)
In-Reply-To: <20230411183418.1640500-3-thuth@redhat.com>
On 4/11/23 20:34, Thomas Huth wrote:
> In some cases of target independent code, it would be useful to have access
> to the functions that swap endianess in case it differs between guest and
> host. Thus re-implement the tswapXX() functions in a new header that can be
> included separately. The check whether the swapping is needed continues to
> be done at compile-time for target specific code, while it is done at
> run-time in target-independent code.
Reviewed-by: Cédric Le Goater <clg@kaod.org>
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
> include/exec/cpu-all.h | 64 +------------------------------------
> include/exec/tswap.h | 72 ++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 73 insertions(+), 63 deletions(-)
> create mode 100644 include/exec/tswap.h
>
> diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h
> index 090922e4a8..ad824fee52 100644
> --- a/include/exec/cpu-all.h
> +++ b/include/exec/cpu-all.h
> @@ -21,6 +21,7 @@
>
> #include "exec/cpu-common.h"
> #include "exec/memory.h"
> +#include "exec/tswap.h"
> #include "qemu/thread.h"
> #include "hw/core/cpu.h"
> #include "qemu/rcu.h"
> @@ -44,69 +45,6 @@
> #define BSWAP_NEEDED
I wonder if we could get rid of the BSWAP_NEEDED define also.
(used load_aout() and ELF loaders)
Thanks,
C.
> #endif
>
> -#ifdef BSWAP_NEEDED
> -
> -static inline uint16_t tswap16(uint16_t s)
> -{
> - return bswap16(s);
> -}
> -
> -static inline uint32_t tswap32(uint32_t s)
> -{
> - return bswap32(s);
> -}
> -
> -static inline uint64_t tswap64(uint64_t s)
> -{
> - return bswap64(s);
> -}
> -
> -static inline void tswap16s(uint16_t *s)
> -{
> - *s = bswap16(*s);
> -}
> -
> -static inline void tswap32s(uint32_t *s)
> -{
> - *s = bswap32(*s);
> -}
> -
> -static inline void tswap64s(uint64_t *s)
> -{
> - *s = bswap64(*s);
> -}
> -
> -#else
> -
> -static inline uint16_t tswap16(uint16_t s)
> -{
> - return s;
> -}
> -
> -static inline uint32_t tswap32(uint32_t s)
> -{
> - return s;
> -}
> -
> -static inline uint64_t tswap64(uint64_t s)
> -{
> - return s;
> -}
> -
> -static inline void tswap16s(uint16_t *s)
> -{
> -}
> -
> -static inline void tswap32s(uint32_t *s)
> -{
> -}
> -
> -static inline void tswap64s(uint64_t *s)
> -{
> -}
> -
> -#endif
> -
> #if TARGET_LONG_SIZE == 4
> #define tswapl(s) tswap32(s)
> #define tswapls(s) tswap32s((uint32_t *)(s))
> diff --git a/include/exec/tswap.h b/include/exec/tswap.h
> new file mode 100644
> index 0000000000..68944a880b
> --- /dev/null
> +++ b/include/exec/tswap.h
> @@ -0,0 +1,72 @@
> +/*
> + * Macros for swapping a value if the endianness is different
> + * between the target and the host.
> + *
> + * SPDX-License-Identifier: LGPL-2.1-or-later
> + */
> +
> +#ifndef TSWAP_H
> +#define TSWAP_H
> +
> +#include "hw/core/cpu.h"
> +#include "qemu/bswap.h"
> +
> +/*
> + * If we're in target-specific code, we can hard-code the swapping
> + * condition, otherwise we have to do (slower) run-time checks.
> + */
> +#ifdef NEED_CPU_H
> +#define target_needs_bswap() (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN)
> +#else
> +#define target_needs_bswap() (target_words_bigendian() != HOST_BIG_ENDIAN)
> +#endif
> +
> +static inline uint16_t tswap16(uint16_t s)
> +{
> + if (target_needs_bswap()) {
> + return bswap16(s);
> + } else {
> + return s;
> + }
> +}
> +
> +static inline uint32_t tswap32(uint32_t s)
> +{
> + if (target_needs_bswap()) {
> + return bswap32(s);
> + } else {
> + return s;
> + }
> +}
> +
> +static inline uint64_t tswap64(uint64_t s)
> +{
> + if (target_needs_bswap()) {
> + return bswap64(s);
> + } else {
> + return s;
> + }
> +}
> +
> +static inline void tswap16s(uint16_t *s)
> +{
> + if (target_needs_bswap()) {
> + *s = bswap16(*s);
> + }
> +}
> +
> +static inline void tswap32s(uint32_t *s)
> +{
> + if (target_needs_bswap()) {
> + *s = bswap32(*s);
> + }
> +}
> +
> +static inline void tswap64s(uint64_t *s)
> +{
> + if (target_needs_bswap()) {
> + *s = bswap64(*s);
> + }
> +}
> +
> +#endif /* TSWAP_H */
next prev parent reply other threads:[~2023-04-12 11:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-11 18:34 [PATCH for-8.1 0/3] Make softmmu/qtest.c target independent Thomas Huth
2023-04-11 18:34 ` [PATCH 1/3] softmmu/qtest: Move the target-specific pseries RTAS code out of qtest.c Thomas Huth
2023-04-12 10:44 ` Cédric Le Goater
2023-04-11 18:34 ` [PATCH 2/3] include/exec: Provide the tswap() functions for target independent code, too Thomas Huth
2023-04-12 11:01 ` Cédric Le Goater [this message]
2023-04-11 18:34 ` [PATCH 3/3] softmmu: Make qtest.c target independent Thomas Huth
2023-04-12 10:53 ` Cédric Le Goater
2023-04-12 10:13 ` [PATCH for-8.1 0/3] Make softmmu/qtest.c " Richard Henderson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b4f80d34-886c-6fb5-587f-728cde13f57d@kaod.org \
--to=clg@kaod.org \
--cc=danielhb413@gmail.com \
--cc=david@gibson.dropbear.id.au \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).