include/exec: Provide the cpu_internal_tswap() functions
These functions are needed to support semihosting on CPUs that support runtime-configurable endianness. They should not be used in other contexts. Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de> Message-id: 20260311-semihosting-cpu-tswap-v3-2-8e239575ec08@eonerc.rwth-aachen.de Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Martin Kröning committed
Mar 11, 2026 at 17:27 UTC
c24e94fd1564470fa09be63490c4251a4e252fd9
1 file changed
+36
include/exec/tswap.h
+36
@@ -10,6 +10,7 @@
10
11
#include "qemu/bswap.h"
12
#include "qemu/target-info.h"
13
+#include "hw/core/cpu.h"
14
15
/*
16
* If we're in target-specific code, we can hard-code the swapping
@@ -72,4 +73,39 @@ static inline void tswap64s(uint64_t *s)
73
}
74
#endif
75
76
+/*
77
+ * If we're in semihosting code, have to swap depending on the currently
78
+ * configured endianness of the CPU. These functions should not be used in
79
+ * other contexts.
80
+ */
81
+#define cpu_internal_needs_bswap(cpu) \
82
+ (HOST_BIG_ENDIAN != cpu_internal_is_big_endian(cpu))
83
+
84
+static inline uint16_t cpu_internal_tswap16(CPUState *cpu, uint16_t s)
85
+{
86
+ if (cpu_internal_needs_bswap(cpu)) {
87
+ return bswap16(s);
88
+ } else {
89
+ return s;
90
+ }
91
+}
92
+
93
+static inline uint32_t cpu_internal_tswap32(CPUState *cpu, uint32_t s)
94
+{
95
+ if (cpu_internal_needs_bswap(cpu)) {
96
+ return bswap32(s);
97
+ } else {
98
+ return s;
99
+ }
100
+}
101
+
102
+static inline uint64_t cpu_internal_tswap64(CPUState *cpu, uint64_t s)
103
+{
104
+ if (cpu_internal_needs_bswap(cpu)) {
105
+ return bswap64(s);
106
+ } else {
107
+ return s;
108
+ }
109
+}
110
+
111
#endif /* TSWAP_H */