Skip to contentSkip to conversation

Implement byteswap and update references

PATCH Superseded Lin Liu 4 participants 16 messages Oct 22, 2021 – Nov 2, 2021

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Lin Liu

author
#0 lore raw

v2 cover Implement byteswap and update references

The swab() is massively over complicated
Simplify it with compiler builtins and fallback to plain C function if undefined.
Update components to switch to this new swap bytes.

Lin Liu (7):
xen: implement byteswap.h
crypto/vmac: Simplify code with byteswap.h
arm64/find_next_bit: Remove ext2_swab()
arm: Switch to byteswap.h
xen/xsm: Switch to byteswap.h
xen: Switch to byteswap.h
byteorder: Remove byteorder

 xen/arch/arm/alternative.c                |   2 +-
 xen/arch/arm/arm64/lib/find_next_bit.c    |  40 +----
 xen/arch/arm/arm64/livepatch.c            |   2 +-
 xen/arch/arm/kernel.c                     |   2 +-
 xen/arch/arm/vgic/vgic-mmio.c             |   2 +-
 xen/common/bitmap.c                       |   2 +-
 xen/common/gdbstub.c                      |   2 +-
 xen/common/libelf/libelf-private.h        |   8 +-
 xen/common/lz4/defs.h                     |   2 +-
 xen/common/lzo.c                          |   2 +-
 xen/common/unlzo.c                        |   2 +-
 xen/common/xz/private.h                   |   4 +-
 xen/crypto/vmac.c                         |  76 +--------
 xen/drivers/char/ehci-dbgp.c              |   2 +-
 xen/include/asm-arm/arm32/io.h            |   2 +-
 xen/include/asm-arm/arm64/io.h            |   2 +-
 xen/include/asm-arm/byteorder.h           |  16 --
 xen/include/asm-x86/byteorder.h           |  36 -----
 xen/include/asm-x86/msi.h                 |   2 +-
 xen/include/xen/bitmap.h                  |   2 +-
 xen/include/xen/byteorder/big_endian.h    | 102 ------------
 xen/include/xen/byteorder/generic.h       |  68 --------
 xen/include/xen/byteorder/little_endian.h | 102 ------------
 xen/include/xen/byteorder/swab.h          | 183 ----------------------
 xen/include/xen/byteswap.h                |  93 +++++++++++
 xen/include/xen/compiler.h                |  12 ++
 xen/include/xen/device_tree.h             |   2 +-
 xen/include/xen/libfdt/libfdt_env.h       |   2 +-
 xen/include/xen/unaligned.h               |  14 +-
 xen/lib/divmod.c                          |   2 +-
 xen/xsm/flask/ss/avtab.c                  |   2 +-
 xen/xsm/flask/ss/conditional.c            |   2 +-
 xen/xsm/flask/ss/ebitmap.c                |   2 +-
 xen/xsm/flask/ss/policydb.c               |   2 +-
 34 files changed, 150 insertions(+), 646 deletions(-)
 delete mode 100644 xen/include/asm-arm/byteorder.h
 delete mode 100644 xen/include/asm-x86/byteorder.h
 delete mode 100644 xen/include/xen/byteorder/big_endian.h
 delete mode 100644 xen/include/xen/byteorder/generic.h
 delete mode 100644 xen/include/xen/byteorder/little_endian.h
 delete mode 100644 xen/include/xen/byteorder/swab.h
 create mode 100644 xen/include/xen/byteswap.h
Signature · 1 line
2.27.0
author
#1 lore raw

PATCH v2 1/7 xen: implement byteswap.h

swab() is massively over complicated and can be simplified by builtins. The compilers provide builtin function to swap bytes.
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
* clang: https://clang.llvm.org/docs/LanguageExtensions.html
This patch simplify swab() with builtins and fallback for old compilers.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
Changes in v2:
- Add fallback for compilers without __builtin_bswap
- Implement with plain C instead of macros

---
 xen/include/xen/byteswap.h | 93 ++++++++++++++++++++++++++++++++++++++
 xen/include/xen/compiler.h | 12 +++++
 2 files changed, 105 insertions(+)
 create mode 100644 xen/include/xen/byteswap.h
xen/include/xen/byteswap.h new file +93 -0
new file mode 100644
index 0000000000..848a4bbaee
@@ -0,0 +1,93 @@
+#ifndef _BYTESWAP_H
+#define _BYTESWAP_H
+
+#include <xen/types.h>
+
+#if !__has_builtin(__builtin_bswap16)
+static always_inline uint16_t __builtin_bswap16(uint16_t val)
+{
+    return ((val & 0x00FF) << 8) | ((val & 0xFF00) >> 8);
+}
+#endif
+
+#if !__has_builtin(__builtin_bswap32)
+static always_inline uint32_t __builtin_bswap32(uint32_t val)
+{
+    return ((val & 0x000000FF) << 24) |
+           ((val & 0x0000FF00) <<  8) |
+           ((val & 0x00FF0000) >>  8) |
+           ((val & 0xFF000000) >> 24);
+}
+#endif
+
+#if !__has_builtin(__builtin_bswap64)
+static always_inline uint64_t __builtin_bswap64(uint64_t val)
+{
+    return ((val & 0x00000000000000FF) << 56) |
+           ((val & 0x000000000000FF00) << 40) |
+           ((val & 0x0000000000FF0000) << 24) |
+           ((val & 0x00000000FF000000) <<  8) |
+           ((val & 0x000000FF00000000) >>  8) |
+           ((val & 0x0000FF0000000000) >> 24) |
+           ((val & 0x00FF000000000000) >> 40) |
+           ((val & 0xFF00000000000000) >> 56);
+}
+#endif
+
+#define bswap16(x) __builtin_bswap16(x)
+#define bswap32(x) __builtin_bswap32(x)
+#define bswap64(x) __builtin_bswap64(x)
+
+#define bswap_ul(x) bswap##BITS_PER_LONG(x)
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+
+#  ifndef __LITTLE_ENDIAN
+#    define __LITTLE_ENDIAN 1234
+#  endif
+
+#  ifndef __LITTLE_ENDIAN_BITFIELD
+#    define __LITTLE_ENDIAN_BITFIELD
+#  endif
+
+#  define cpu_to_le64(x) (x)
+#  define le64_to_cpu(x) (x)
+#  define cpu_to_le32(x) (x)
+#  define le32_to_cpu(x) (x)
+#  define cpu_to_le16(x) (x)
+#  define le16_to_cpu(x) (x)
+#  define cpu_to_be64(x) bswap64(x)
+#  define be64_to_cpu(x) bswap64(x)
+#  define cpu_to_be32(x) bswap32(x)
+#  define be32_to_cpu(x) bswap32(x)
+#  define cpu_to_be16(x) bswap16(x)
+#  define be16_to_cpu(x) bswap16(x)
+
+#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+
+#  ifndef __BIG_ENDIAN
+#    define __BIG_ENDIAN 4321
+#  endif
+
+#  ifndef __BIG_ENDIAN_BITFIELD
+#    define __BIG_ENDIAN_BITFIELD
+#  endif
+
+#  define cpu_to_le64(x) bswap64(x)
+#  define le64_to_cpu(x) bswap64(x)
+#  define cpu_to_le32(x) bswap32(x)
+#  define le32_to_cpu(x) bswap32(x)
+#  define cpu_to_le16(x) bswap16(x)
+#  define le16_to_cpu(x) bswap16(x)
+#  define cpu_to_be64(x) (x)
+#  define be64_to_cpu(x) (x)
+#  define cpu_to_be32(x) (x)
+#  define be32_to_cpu(x) (x)
+#  define cpu_to_be16(x) (x)
+#  define be16_to_cpu(x) (x)
+
+#else
+#  error "Unknown Endianness"
+#endif /* __BYTE_ORDER__ */
+
+#endif /* _LINUX_BITOPS_H */
xen/include/xen/compiler.h +12 -0
index 696c7eb89e..68f28082a5 100644
@@ -179,4 +179,16 @@
 # define CLANG_DISABLE_WARN_GCC_COMPAT_END
 #endif
 
+#if (!defined(__clang__) && (__GNUC__ < 10))
+/*
+ * Backwards compatibility for GCC < 10.
+ * All supported versions of Clang support __has_builtin
+ * */
+#define __has_builtin(x) GCC_has ## x
+
+#define GCC_has__builtin_bswap16 (CONFIG_GCC_VERSION >= 40800)
+#define GCC_has__builtin_bswap32 (CONFIG_GCC_VERSION >= 40400)
+#define GCC_has__builtin_bswap64 (CONFIG_GCC_VERSION >= 40400)
+#endif
+
 #endif /* __LINUX_COMPILER_H */
Signature · 1 line
2.27.0
author
#8 lore raw

PATCH v2 5/7 xen/xsm: Switch to byteswap.h

In reply to Lin Liu

Update to use byteswap.h to swap bytes

No functional change

---
Cc: Daniel De Graaf <[email protected]>
Cc: "Daniel P. Smith" <[email protected]>

---
 xen/xsm/flask/ss/avtab.c       | 2 +-
 xen/xsm/flask/ss/conditional.c | 2 +-
 xen/xsm/flask/ss/ebitmap.c     | 2 +-
 xen/xsm/flask/ss/policydb.c    | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
xen/xsm/flask/ss/avtab.c +1 -1
index bfc91c8b0c..1fa796f625 100644
@@ -19,8 +19,8 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
+#include <xen/byteswap.h>
 #include <xen/lib.h>
-#include <asm/byteorder.h>
 #include <xen/types.h>
 #include <xen/xmalloc.h>
 #include <xen/errno.h>
xen/xsm/flask/ss/conditional.c +1 -1
index 3e58aea551..059f6e07e5 100644
@@ -9,7 +9,7 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/types.h>
 #include <xen/errno.h>
xen/xsm/flask/ss/ebitmap.c +1 -1
index e1d0a586a7..1550437c6f 100644
@@ -10,7 +10,7 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/xmalloc.h>
 #include <xen/errno.h>
xen/xsm/flask/ss/policydb.c +1 -1
index 9426164353..595005c3b7 100644
@@ -22,7 +22,7 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/types.h>
 #include <xen/xmalloc.h>
Signature · 1 line
2.27.0
author
#7 lore raw

PATCH v2 4/7 arm: Switch to byteswap.h

In reply to Lin Liu

Update to use byteswap.h to swap bytes.

No functional change.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Ross Lagerwall <[email protected]>

---
 xen/arch/arm/alternative.c          | 2 +-
 xen/arch/arm/arm64/livepatch.c      | 2 +-
 xen/arch/arm/kernel.c               | 2 +-
 xen/arch/arm/vgic/vgic-mmio.c       | 2 +-
 xen/include/asm-arm/arm32/io.h      | 2 +-
 xen/include/asm-arm/arm64/io.h      | 2 +-
 xen/include/xen/libfdt/libfdt_env.h | 2 +-
 7 files changed, 7 insertions(+), 7 deletions(-)
xen/arch/arm/alternative.c +1 -1
index 237c4e5642..0f84260ac9 100644
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <xen/byteswap.h>
 #include <xen/init.h>
 #include <xen/types.h>
 #include <xen/kernel.h>
@@ -27,7 +28,6 @@
 #include <xen/virtual_region.h>
 #include <asm/alternative.h>
 #include <asm/atomic.h>
-#include <asm/byteorder.h>
 #include <asm/cpufeature.h>
 #include <asm/insn.h>
 #include <asm/page.h>
xen/arch/arm/arm64/livepatch.c +1 -1
index 6ec8dc60f0..26b5729edf 100644
@@ -3,6 +3,7 @@
  */
 
 #include <xen/bitops.h>
+#include <xen/byteswap.h>
 #include <xen/errno.h>
 #include <xen/lib.h>
 #include <xen/livepatch_elf.h>
@@ -11,7 +12,6 @@
 #include <xen/vmap.h>
 
 #include <asm/bitops.h>
-#include <asm/byteorder.h>
 #include <asm/insn.h>
 #include <asm/livepatch.h>
 
xen/arch/arm/kernel.c +1 -1
index 8f43caa186..e89bb9cef8 100644
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2011 Citrix Systems, Inc.
  */
+#include <xen/byteswap.h>
 #include <xen/domain_page.h>
 #include <xen/errno.h>
 #include <xen/guest_access.h>
@@ -14,7 +15,6 @@
 #include <xen/sched.h>
 #include <xen/vmap.h>
 
-#include <asm/byteorder.h>
 #include <asm/kernel.h>
 #include <asm/setup.h>
 
xen/arch/arm/vgic/vgic-mmio.c +1 -1
index 5d935a7301..d9c5066246 100644
@@ -13,10 +13,10 @@
  */
 
 #include <xen/bitops.h>
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/sched.h>
 #include <asm/new_vgic.h>
-#include <asm/byteorder.h>
 
 #include "vgic.h"
 #include "vgic-mmio.h"
xen/include/asm-arm/arm32/io.h +1 -1
index 73a879e9fb..df8547403c 100644
@@ -21,8 +21,8 @@
 #ifndef _ARM_ARM32_IO_H
 #define _ARM_ARM32_IO_H
 
+#include <xen/byteswap.h>
 #include <asm/system.h>
-#include <asm/byteorder.h>
 
 static inline void __raw_writeb(u8 val, volatile void __iomem *addr)
 {
xen/include/asm-arm/arm64/io.h +1 -1
index 30bfc78d9e..db328d9e3c 100644
@@ -20,8 +20,8 @@
 #ifndef _ARM_ARM64_IO_H
 #define _ARM_ARM64_IO_H
 
+#include <xen/byteswap.h>
 #include <asm/system.h>
-#include <asm/byteorder.h>
 #include <asm/alternative.h>
 
 /*
xen/include/xen/libfdt/libfdt_env.h +1 -1
index 035bf754d2..f8ea1ea07a 100644
@@ -1,9 +1,9 @@
 #ifndef _LIBFDT_ENV_H
 #define _LIBFDT_ENV_H
 
+#include <xen/byteswap.h>
 #include <xen/types.h>
 #include <xen/string.h>
-#include <asm/byteorder.h>
 
 typedef uint16_t fdt16_t;
 typedef uint32_t fdt32_t;
Signature · 1 line
2.27.0
author
#6 lore raw

PATCH v2 3/7 arm64/find_next_bit: Remove ext2_swab()

In reply to Lin Liu

ext2 has nothing to do with this logic. Clean up the code with xen/byteswap.h which now has an unsigned long helper.

No functional change.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Volodymyr Babchuk <[email protected]> The patches are not well tested without running environment

---
 xen/arch/arm/arm64/lib/find_next_bit.c | 40 ++++++--------------------
 1 file changed, 8 insertions(+), 32 deletions(-)
xen/arch/arm/arm64/lib/find_next_bit.c +8 -32
index 8ebf8bfe97..8a7cfc0949 100644
@@ -8,9 +8,9 @@
  * as published by the Free Software Foundation; either version
  * 2 of the License, or (at your option) any later version.
  */
-#include <xen/bitops.h>
 #include <asm/types.h>
-#include <asm/byteorder.h>
+#include <xen/bitops.h>
+#include <xen/byteswap.h>
 
 #ifndef find_next_bit
 /*
@@ -161,30 +161,6 @@ EXPORT_SYMBOL(find_first_zero_bit);
 
 #ifdef __BIG_ENDIAN
 
-/* include/linux/byteorder does not support "unsigned long" type */
-static inline unsigned long ext2_swabp(const unsigned long * x)
-{
-#if BITS_PER_LONG == 64
-	return (unsigned long) __swab64p((u64 *) x);
-#elif BITS_PER_LONG == 32
-	return (unsigned long) __swab32p((u32 *) x);
-#else
-#error BITS_PER_LONG not defined
-#endif
-}
-
-/* include/linux/byteorder doesn't support "unsigned long" type */
-static inline unsigned long ext2_swab(const unsigned long y)
-{
-#if BITS_PER_LONG == 64
-	return (unsigned long) __swab64((u64) y);
-#elif BITS_PER_LONG == 32
-	return (unsigned long) __swab32((u32) y);
-#else
-#error BITS_PER_LONG not defined
-#endif
-}
-
 #ifndef find_next_zero_bit_le
 unsigned long find_next_zero_bit_le(const void *addr, unsigned
 		long size, unsigned long offset)
@@ -199,7 +175,7 @@ unsigned long find_next_zero_bit_le(const void *addr, unsigned
 	size -= result;
 	offset &= (BITS_PER_LONG - 1UL);
 	if (offset) {
-		tmp = ext2_swabp(p++);
+		tmp = bswap_ul(*p++);
 		tmp |= (~0UL >> (BITS_PER_LONG - offset));
 		if (size < BITS_PER_LONG)
 			goto found_first;
@@ -217,7 +193,7 @@ unsigned long find_next_zero_bit_le(const void *addr, unsigned
 	}
 	if (!size)
 		return result;
-	tmp = ext2_swabp(p);
+	tmp = bswap_ul(*p);
 found_first:
 	tmp |= ~0UL << size;
 	if (tmp == ~0UL)	/* Are any bits zero? */
@@ -226,7 +202,7 @@ found_middle:
 	return result + ffz(tmp);
 
 found_middle_swap:
-	return result + ffz(ext2_swab(tmp));
+	return result + ffz(bswap_ul(tmp));
 }
 EXPORT_SYMBOL(find_next_zero_bit_le);
 #endif
@@ -245,7 +221,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned
 	size -= result;
 	offset &= (BITS_PER_LONG - 1UL);
 	if (offset) {
-		tmp = ext2_swabp(p++);
+		tmp = bswap_ul(*p++);
 		tmp &= (~0UL << offset);
 		if (size < BITS_PER_LONG)
 			goto found_first;
@@ -264,7 +240,7 @@ unsigned long find_next_bit_le(const void *addr, unsigned
 	}
 	if (!size)
 		return result;
-	tmp = ext2_swabp(p);
+	tmp = bswap_ul(*p);
 found_first:
 	tmp &= (~0UL >> (BITS_PER_LONG - size));
 	if (tmp == 0UL)		/* Are any bits set? */
@@ -273,7 +249,7 @@ found_middle:
 	return result + __ffs(tmp);
 
 found_middle_swap:
-	return result + __ffs(ext2_swab(tmp));
+	return result + __ffs(bswap_ul(tmp));
 }
 EXPORT_SYMBOL(find_next_bit_le);
 #endif
Signature · 1 line
2.27.0
author
#14 lore raw

PATCH v2 7/7 byteorder: Remove byteorder

In reply to Lin Liu

include/xen/byteswap.h has simplify the interface, just clean the old interface

No functional change

---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

---
 xen/include/asm-arm/byteorder.h           |  16 --
 xen/include/asm-x86/byteorder.h           |  36 -----
 xen/include/xen/byteorder/big_endian.h    | 102 ------------
 xen/include/xen/byteorder/generic.h       |  68 --------
 xen/include/xen/byteorder/little_endian.h | 102 ------------
 xen/include/xen/byteorder/swab.h          | 183 ----------------------
 6 files changed, 507 deletions(-)
 delete mode 100644 xen/include/asm-arm/byteorder.h
 delete mode 100644 xen/include/asm-x86/byteorder.h
 delete mode 100644 xen/include/xen/byteorder/big_endian.h
 delete mode 100644 xen/include/xen/byteorder/generic.h
 delete mode 100644 xen/include/xen/byteorder/little_endian.h
 delete mode 100644 xen/include/xen/byteorder/swab.h
xen/include/asm-arm/byteorder.h deleted +0 -16
deleted file mode 100644
index 9c712c4788..0000000000
@@ -1,16 +0,0 @@
-#ifndef __ASM_ARM_BYTEORDER_H__
-#define __ASM_ARM_BYTEORDER_H__
-
-#define __BYTEORDER_HAS_U64__
-
-#include <xen/byteorder/little_endian.h>
-
-#endif /* __ASM_ARM_BYTEORDER_H__ */
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- */
xen/include/asm-x86/byteorder.h deleted +0 -36
deleted file mode 100644
index 1f77e502a5..0000000000
@@ -1,36 +0,0 @@
-#ifndef __ASM_X86_BYTEORDER_H__
-#define __ASM_X86_BYTEORDER_H__
-
-#include <asm/types.h>
-#include <xen/compiler.h>
-
-static inline __attribute_const__ __u32 ___arch__swab32(__u32 x)
-{
-    asm("bswap %0" : "=r" (x) : "0" (x));
-    return x;
-}
-
-static inline __attribute_const__ __u64 ___arch__swab64(__u64 val)
-{ 
-    union { 
-        struct { __u32 a,b; } s;
-        __u64 u;
-    } v;
-    v.u = val;
-    asm("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
-        : "=r" (v.s.a), "=r" (v.s.b) 
-        : "0" (v.s.a), "1" (v.s.b)); 
-    return v.u;
-} 
-
-/* Do not define swab16.  Gcc is smart enough to recognize "C" version and
-   convert it into rotation or exhange.  */
-
-#define __arch__swab64(x) ___arch__swab64(x)
-#define __arch__swab32(x) ___arch__swab32(x)
-
-#define __BYTEORDER_HAS_U64__
-
-#include <xen/byteorder/little_endian.h>
-
-#endif /* __ASM_X86_BYTEORDER_H__ */
xen/include/xen/byteorder/big_endian.h deleted +0 -102

102 lines not on this page · raw message

xen/include/xen/byteorder/generic.h deleted +0 -68

68 lines not on this page · raw message

xen/include/xen/byteorder/little_endian.h deleted +0 -102

102 lines not on this page · raw message

xen/include/xen/byteorder/swab.h deleted +0 -183

183 lines not on this page · raw message

Signature · 1 line
2.27.0
author
#4 lore raw

PATCH v2 2/7 crypto/vmac: Simplify code with byteswap.h

In reply to Lin Liu

This file has its own implementation of swap bytes. Clean up the code with xen/byteswap.h.

No functional change.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>

---
 xen/crypto/vmac.c | 76 ++---------------------------------------------
 1 file changed, 3 insertions(+), 73 deletions(-)
xen/crypto/vmac.c +3 -73
index 294dd16a52..acb4e015f5 100644
@@ -8,6 +8,7 @@
 
 /* start for Xen */
 #include <xen/init.h>
+#include <xen/byteswap.h>
 #include <xen/types.h>
 #include <xen/lib.h>
 #include <crypto/vmac.h>
@@ -50,7 +51,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff);  /* Poly key mask     */
  * MUL64: 64x64->128-bit multiplication
  * PMUL64: assumes top bits cleared on inputs
  * ADD128: 128x128->128-bit addition
- * GET_REVERSED_64: load and byte-reverse 64-bit word  
  * ----------------------------------------------------------------------- */
 
 /* ----------------------------------------------------------------------- */
@@ -68,22 +68,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff);  /* Poly key mask     */
 
 #define PMUL64 MUL64
 
-#define GET_REVERSED_64(p)                                                \
-    ({uint64_t x;                                                         \
-     asm ("bswapq %0" : "=r" (x) : "0"(*(uint64_t *)(p))); x;})
-
-/* ----------------------------------------------------------------------- */
-#elif (__GNUC__ && __i386__)
-/* ----------------------------------------------------------------------- */
-
-#define GET_REVERSED_64(p)                                                \
-    ({ uint64_t x;                                                        \
-    uint32_t *tp = (uint32_t *)(p);                                       \
-    asm  ("bswap %%edx\n\t"                                               \
-          "bswap %%eax"                                                   \
-    : "=A"(x)                                                             \
-    : "a"(tp[1]), "d"(tp[0]));                                            \
-    x; })
 
 /* ----------------------------------------------------------------------- */
 #elif (__GNUC__ && __ppc64__)
@@ -103,37 +87,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff);  /* Poly key mask     */
 
 #define PMUL64 MUL64
 
-#define GET_REVERSED_64(p)                                                \
-    ({ uint32_t hi, lo, *_p = (uint32_t *)(p);                            \
-       asm volatile ("lwbrx %0, %1, %2" : "=r"(lo) : "b%"(0), "r"(_p) );  \
-       asm volatile ("lwbrx %0, %1, %2" : "=r"(hi) : "b%"(4), "r"(_p) );  \
-       ((uint64_t)hi << 32) | (uint64_t)lo; } )
-
-/* ----------------------------------------------------------------------- */
-#elif (__GNUC__ && (__ppc__ || __PPC__))
-/* ----------------------------------------------------------------------- */
-
-#define GET_REVERSED_64(p)                                                \
-    ({ uint32_t hi, lo, *_p = (uint32_t *)(p);                            \
-       asm volatile ("lwbrx %0, %1, %2" : "=r"(lo) : "b%"(0), "r"(_p) );  \
-       asm volatile ("lwbrx %0, %1, %2" : "=r"(hi) : "b%"(4), "r"(_p) );  \
-       ((uint64_t)hi << 32) | (uint64_t)lo; } )
-
-/* ----------------------------------------------------------------------- */
-#elif (__GNUC__ && (__ARMEL__ || __ARM__))
-/* ----------------------------------------------------------------------- */
-
-#define bswap32(v)                                                        \
-({ uint32_t tmp,out;                                                      \
-    asm volatile(                                                         \
-        "eor    %1, %2, %2, ror #16\n"                                    \
-        "bic    %1, %1, #0x00ff0000\n"                                    \
-        "mov    %0, %2, ror #8\n"                                         \
-        "eor    %0, %0, %1, lsr #8"                                       \
-    : "=r" (out), "=&r" (tmp)                                             \
-    : "r" (v));                                                           \
-    out;})
-
 /* ----------------------------------------------------------------------- */
 #elif _MSC_VER
 /* ----------------------------------------------------------------------- */
@@ -154,11 +107,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff);  /* Poly key mask     */
         (rh) += (ih) + ((rl) < (_il));                               \
     }
 
-#if _MSC_VER >= 1300
-#define GET_REVERSED_64(p) _byteswap_uint64(*(uint64_t *)(p))
-#pragma intrinsic(_byteswap_uint64)
-#endif
-
 #if _MSC_VER >= 1400 && \
     (!defined(__INTEL_COMPILER) || __INTEL_COMPILER >= 1000)
 #define MUL32(i1,i2)    (__emulu((uint32_t)(i1),(uint32_t)(i2)))
@@ -219,24 +167,6 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff);  /* Poly key mask     */
     }
 #endif
 
-#ifndef GET_REVERSED_64
-#ifndef bswap64
-#ifndef bswap32
-#define bswap32(x)                                                        \
-  ({ uint32_t bsx = (x);                                                  \
-      ((((bsx) & 0xff000000u) >> 24) | (((bsx) & 0x00ff0000u) >>  8) |    \
-       (((bsx) & 0x0000ff00u) <<  8) | (((bsx) & 0x000000ffu) << 24)); })
-#endif
-#define bswap64(x)                                                        \
-     ({ union { uint64_t ll; uint32_t l[2]; } w, r;                       \
-         w.ll = (x);                                                      \
-         r.l[0] = bswap32 (w.l[1]);                                       \
-         r.l[1] = bswap32 (w.l[0]);                                       \
-         r.ll; })
-#endif
-#define GET_REVERSED_64(p) bswap64(*(uint64_t *)(p)) 
-#endif
-
 /* ----------------------------------------------------------------------- */
 
 #if (VMAC_PREFER_BIG_ENDIAN)
@@ -247,9 +177,9 @@ const uint64_t mpoly = UINT64_C(0x1fffffff1fffffff);  /* Poly key mask     */
 
 #if (VMAC_ARCH_BIG_ENDIAN)
 #  define get64BE(ptr) (*(uint64_t *)(ptr))
-#  define get64LE(ptr) GET_REVERSED_64(ptr)
+#  define get64LE(ptr) bswap64(*(uint64_t *)(ptr))
 #else /* assume little-endian */
-#  define get64BE(ptr) GET_REVERSED_64(ptr)
+#  define get64BE(ptr) bswap64(*(uint64_t *)(ptr))
 #  define get64LE(ptr) (*(uint64_t *)(ptr))
 #endif
 
Signature · 1 line
2.27.0
author
#11 lore raw

PATCH v2 6/7 xen: Switch to byteswap.h

In reply to Lin Liu

Update to use byteswap.h to swap bytes.

No functional chagne.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

---
 xen/common/bitmap.c                |  2 +-
 xen/common/gdbstub.c               |  2 +-
 xen/common/libelf/libelf-private.h |  8 ++++----
 xen/common/lz4/defs.h              |  2 +-
 xen/common/lzo.c                   |  2 +-
 xen/common/unlzo.c                 |  2 +-
 xen/common/xz/private.h            |  4 ++--
 xen/drivers/char/ehci-dbgp.c       |  2 +-
 xen/include/asm-x86/msi.h          |  2 +-
 xen/include/xen/bitmap.h           |  2 +-
 xen/include/xen/device_tree.h      |  2 +-
 xen/include/xen/unaligned.h        | 14 +++++++-------
 xen/lib/divmod.c                   |  2 +-
 13 files changed, 23 insertions(+), 23 deletions(-)
xen/common/bitmap.c +1 -1
index 7d4551f782..be274ca04a 100644
@@ -9,10 +9,10 @@
 #include <xen/errno.h>
 #include <xen/bitmap.h>
 #include <xen/bitops.h>
+#include <xen/byteswap.h>
 #include <xen/cpumask.h>
 #include <xen/guest_access.h>
 #include <xen/lib.h>
-#include <asm/byteorder.h>
 
 /*
  * bitmaps provide an array of bits, implemented using an an
xen/common/gdbstub.c +1 -1
index 848c1f4327..3c8ed52d6b 100644
@@ -33,6 +33,7 @@
 /* Resuming after we've stopped used to work, but more through luck
    than any actual intention.  It doesn't at the moment. */
 
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/spinlock.h>
 #include <xen/serial.h>
@@ -45,7 +46,6 @@
 #include <xen/console.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
-#include <asm/byteorder.h>
 
 /* Printk isn't particularly safe just after we've trapped to the
    debugger. so avoid it. */
xen/common/libelf/libelf-private.h +4 -4
index 47db679966..b7089cb31b 100644
@@ -17,10 +17,10 @@
 
 #ifdef __XEN__
 
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/libelf.h>
 #include <xen/softirq.h>
-#include <asm/byteorder.h>
 #include <public/elfnote.h>
 
 /* we would like to use elf->log_callback but we can't because
@@ -31,9 +31,9 @@
    printk(fmt, ## args )
 
 #define strtoull(str, end, base) simple_strtoull(str, end, base)
-#define bswap_16(x) swab16(x)
-#define bswap_32(x) swab32(x)
-#define bswap_64(x) swab64(x)
+#define bswap_16(x) bswap16(x)
+#define bswap_32(x) bswap32(x)
+#define bswap_64(x) bswap64(x)
 
 #else /* !__XEN__ */
 
xen/common/lz4/defs.h +1 -1
index 10609f5a53..1ce4476478 100644
@@ -9,7 +9,7 @@
  */
 
 #ifdef __XEN__
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 
xen/common/lzo.c +1 -1
index a87c76dded..17be9675f4 100644
@@ -96,7 +96,7 @@
 
 #ifdef __XEN__
 #include <xen/lib.h>
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 #define get_unaligned_le16(_p) (*(u16 *)(_p))
xen/common/unlzo.c +1 -1
index 74056778eb..f908d2a61f 100644
@@ -33,7 +33,7 @@
 #include <xen/lzo.h>
 
 #ifdef __XEN__
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 
xen/common/xz/private.h +2 -2
index 511343fcc2..647f9699a7 100644
@@ -12,7 +12,7 @@
 
 #ifdef __XEN__
 #include <xen/kernel.h>
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 
@@ -28,7 +28,7 @@ static inline void put_unaligned_le32(u32 val, void *p)
 
 #endif
 
-#define get_le32(p) le32_to_cpup((const uint32_t *)(p))
+#define get_le32(p) le32_to_cpu(*(const uint32_t *)(p))
 
 #define false 0
 #define true 1
xen/drivers/char/ehci-dbgp.c +1 -1
index c893d246de..8412da1b11 100644
@@ -5,13 +5,13 @@
  * Linux; see the Linux source for authorship and copyrights.
  */
 
+#include <xen/byteswap.h>
 #include <xen/console.h>
 #include <xen/delay.h>
 #include <xen/errno.h>
 #include <xen/param.h>
 #include <xen/pci.h>
 #include <xen/serial.h>
-#include <asm/byteorder.h>
 #include <asm/io.h>
 #include <asm/fixmap.h>
 #include <public/physdev.h>
xen/include/asm-x86/msi.h +1 -1
index e228b0f3f3..277375183c 100644
@@ -1,9 +1,9 @@
 #ifndef __ASM_MSI_H
 #define __ASM_MSI_H
 
+#include <xen/byteswap.h>
 #include <xen/cpumask.h>
 #include <xen/pci.h>
-#include <asm/byteorder.h>
 #include <asm/hvm/vmx/vmcs.h>
 
 /*
xen/include/xen/bitmap.h +1 -1
index e9175ab54a..c44a1cb63c 100644
@@ -229,7 +229,7 @@ static inline int bitmap_weight(const unsigned long *src, int nbits)
 	return __bitmap_weight(src, nbits);
 }
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 
 #ifdef __LITTLE_ENDIAN
 #define BITMAP_MEM_ALIGNMENT 8
xen/include/xen/device_tree.h +1 -1
index fd6cd00b43..4921e6b142 100644
@@ -10,10 +10,10 @@
 #ifndef __XEN_DEVICE_TREE_H__
 #define __XEN_DEVICE_TREE_H__
 
-#include <asm/byteorder.h>
 #include <asm/device.h>
 #include <public/xen.h>
 #include <public/device_tree_defs.h>
+#include <xen/byteswap.h>
 #include <xen/kernel.h>
 #include <xen/string.h>
 #include <xen/types.h>
xen/include/xen/unaligned.h +7 -7
index 0a2b16d05d..8a9ec8a0ac 100644
@@ -11,8 +11,8 @@
 #define __XEN_UNALIGNED_H__
 
 #ifdef __XEN__
+#include <xen/byteswap.h>
 #include <xen/types.h>
-#include <asm/byteorder.h>
 #endif
 
 #define get_unaligned(p) (*(p))
@@ -20,7 +20,7 @@
 
 static inline uint16_t get_unaligned_be16(const void *p)
 {
-	return be16_to_cpup(p);
+	return be16_to_cpu(*(uint16_t*)p);
 }
 
 static inline void put_unaligned_be16(uint16_t val, void *p)
@@ -30,7 +30,7 @@ static inline void put_unaligned_be16(uint16_t val, void *p)
 
 static inline uint32_t get_unaligned_be32(const void *p)
 {
-	return be32_to_cpup(p);
+	return be32_to_cpu(*(uint32_t*)p);
 }
 
 static inline void put_unaligned_be32(uint32_t val, void *p)
@@ -40,7 +40,7 @@ static inline void put_unaligned_be32(uint32_t val, void *p)
 
 static inline uint64_t get_unaligned_be64(const void *p)
 {
-	return be64_to_cpup(p);
+	return be64_to_cpu(*(uint64_t*)p);
 }
 
 static inline void put_unaligned_be64(uint64_t val, void *p)
@@ -50,7 +50,7 @@ static inline void put_unaligned_be64(uint64_t val, void *p)
 
 static inline uint16_t get_unaligned_le16(const void *p)
 {
-	return le16_to_cpup(p);
+	return le16_to_cpu(*(uint16_t*)p);
 }
 
 static inline void put_unaligned_le16(uint16_t val, void *p)
@@ -60,7 +60,7 @@ static inline void put_unaligned_le16(uint16_t val, void *p)
 
 static inline uint32_t get_unaligned_le32(const void *p)
 {
-	return le32_to_cpup(p);
+	return le32_to_cpu(*(uint32_t*)p);
 }
 
 static inline void put_unaligned_le32(uint32_t val, void *p)
@@ -70,7 +70,7 @@ static inline void put_unaligned_le32(uint32_t val, void *p)
 
 static inline uint64_t get_unaligned_le64(const void *p)
 {
-	return le64_to_cpup(p);
+	return le64_to_cpu(*(uint64_t*)p);
 }
 
 static inline void put_unaligned_le64(uint64_t val, void *p)
xen/lib/divmod.c +1 -1
index 0be6ccc700..dfc1129cfe 100644
@@ -1,6 +1,6 @@
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/types.h>
-#include <asm/byteorder.h>
 
 /*
  * A couple of 64 bit operations ported from FreeBSD.
Signature · 1 line
2.27.0
#12 lore raw

Re: [PATCH v2 6/7] xen: Switch to byteswap.h

Quoted 234 lines · Lin Liu

Update to use byteswap.h to swap bytes.

No functional chagne.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]>

---
 xen/common/bitmap.c                |  2 +-
 xen/common/gdbstub.c               |  2 +-
 xen/common/libelf/libelf-private.h |  8 ++++----
 xen/common/lz4/defs.h              |  2 +-
 xen/common/lzo.c                   |  2 +-
 xen/common/unlzo.c                 |  2 +-
 xen/common/xz/private.h            |  4 ++--
 xen/drivers/char/ehci-dbgp.c       |  2 +-
 xen/include/asm-x86/msi.h          |  2 +-
 xen/include/xen/bitmap.h           |  2 +-
 xen/include/xen/device_tree.h      |  2 +-
 xen/include/xen/unaligned.h        | 14 +++++++-------
 xen/lib/divmod.c                   |  2 +-
 13 files changed, 23 insertions(+), 23 deletions(-)
xen/common/bitmap.c +1 -1
index 7d4551f782..be274ca04a 100644
@@ -9,10 +9,10 @@
 #include <xen/errno.h>
 #include <xen/bitmap.h>
 #include <xen/bitops.h>
+#include <xen/byteswap.h>
 #include <xen/cpumask.h>
 #include <xen/guest_access.h>
 #include <xen/lib.h>
-#include <asm/byteorder.h>
 
 /*
  * bitmaps provide an array of bits, implemented using an an
xen/common/gdbstub.c +1 -1
index 848c1f4327..3c8ed52d6b 100644
@@ -33,6 +33,7 @@
 /* Resuming after we've stopped used to work, but more through luck
    than any actual intention.  It doesn't at the moment. */
 
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/spinlock.h>
 #include <xen/serial.h>
@@ -45,7 +46,6 @@
 #include <xen/console.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
-#include <asm/byteorder.h>
 
 /* Printk isn't particularly safe just after we've trapped to the
    debugger. so avoid it. */
xen/common/libelf/libelf-private.h +4 -4
index 47db679966..b7089cb31b 100644
@@ -17,10 +17,10 @@
 
 #ifdef __XEN__
 
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/libelf.h>
 #include <xen/softirq.h>
-#include <asm/byteorder.h>
 #include <public/elfnote.h>
 
 /* we would like to use elf->log_callback but we can't because
@@ -31,9 +31,9 @@
    printk(fmt, ## args )
 
 #define strtoull(str, end, base) simple_strtoull(str, end, base)
-#define bswap_16(x) swab16(x)
-#define bswap_32(x) swab32(x)
-#define bswap_64(x) swab64(x)
+#define bswap_16(x) bswap16(x)
+#define bswap_32(x) bswap32(x)
+#define bswap_64(x) bswap64(x)
 
 #else /* !__XEN__ */
 
xen/common/lz4/defs.h +1 -1
index 10609f5a53..1ce4476478 100644
@@ -9,7 +9,7 @@
  */
 
 #ifdef __XEN__
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 
xen/common/lzo.c +1 -1
index a87c76dded..17be9675f4 100644
@@ -96,7 +96,7 @@
 
 #ifdef __XEN__
 #include <xen/lib.h>
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 #define get_unaligned_le16(_p) (*(u16 *)(_p))
xen/common/unlzo.c +1 -1
index 74056778eb..f908d2a61f 100644
@@ -33,7 +33,7 @@
 #include <xen/lzo.h>
 
 #ifdef __XEN__
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 
xen/common/xz/private.h +2 -2
index 511343fcc2..647f9699a7 100644
@@ -12,7 +12,7 @@
 
 #ifdef __XEN__
 #include <xen/kernel.h>
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <asm/unaligned.h>
 #else
 
@@ -28,7 +28,7 @@ static inline void put_unaligned_le32(u32 val, void *p)
 
 #endif
 
-#define get_le32(p) le32_to_cpup((const uint32_t *)(p))
+#define get_le32(p) le32_to_cpu(*(const uint32_t *)(p))
 
 #define false 0
 #define true 1
xen/drivers/char/ehci-dbgp.c +1 -1
index c893d246de..8412da1b11 100644
@@ -5,13 +5,13 @@
  * Linux; see the Linux source for authorship and copyrights.
  */
 
+#include <xen/byteswap.h>
 #include <xen/console.h>
 #include <xen/delay.h>
 #include <xen/errno.h>
 #include <xen/param.h>
 #include <xen/pci.h>
 #include <xen/serial.h>
-#include <asm/byteorder.h>
 #include <asm/io.h>
 #include <asm/fixmap.h>
 #include <public/physdev.h>
xen/include/asm-x86/msi.h +1 -1
index e228b0f3f3..277375183c 100644
@@ -1,9 +1,9 @@
 #ifndef __ASM_MSI_H
 #define __ASM_MSI_H
 
+#include <xen/byteswap.h>
 #include <xen/cpumask.h>
 #include <xen/pci.h>
-#include <asm/byteorder.h>
 #include <asm/hvm/vmx/vmcs.h>
 
 /*
xen/include/xen/bitmap.h +1 -1
index e9175ab54a..c44a1cb63c 100644
@@ -229,7 +229,7 @@ static inline int bitmap_weight(const unsigned long *src, int nbits)
 	return __bitmap_weight(src, nbits);
 }
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 
 #ifdef __LITTLE_ENDIAN
 #define BITMAP_MEM_ALIGNMENT 8
xen/include/xen/device_tree.h +1 -1
index fd6cd00b43..4921e6b142 100644
@@ -10,10 +10,10 @@
 #ifndef __XEN_DEVICE_TREE_H__
 #define __XEN_DEVICE_TREE_H__
 
-#include <asm/byteorder.h>
 #include <asm/device.h>
 #include <public/xen.h>
 #include <public/device_tree_defs.h>
+#include <xen/byteswap.h>
 #include <xen/kernel.h>
 #include <xen/string.h>
 #include <xen/types.h>
xen/include/xen/unaligned.h +1 -1
index 0a2b16d05d..8a9ec8a0ac 100644
@@ -11,8 +11,8 @@
 #define __XEN_UNALIGNED_H__
 
 #ifdef __XEN__
+#include <xen/byteswap.h>
 #include <xen/types.h>
-#include <asm/byteorder.h>
 #endif
 
 #define get_unaligned(p) (*(p))

xen/include/xen/unaligned.h line 23

Quoted 5 lines
xen/include/xen/unaligned.h +1 -1
@@ -20,7 +20,7 @@
 
 static inline uint16_t get_unaligned_be16(const void *p)
 {
-	return be16_to_cpup(p);
+	return be16_to_cpu(*(uint16_t*)p);

Hmm - this missed one of my pieces of internal feedback.

*(const uint16_t *)p

to get correct style and avoid casting away const.

Also, the put functions what to drop the __force and use typecasting like this.

I can fix up on commit if there are no other concerns with the series.

~Andrew

#15 lore raw
In reply to Lin Liu
Quoted 7 lines · Lin Liu

The swab() is massively over complicated
Simplify it with compiler builtins and fallback to plain C function if undefined.
Update components to switch to this new swap bytes.

<snip>

 34 files changed, 150 insertions(+), 646 deletions(-)

It is worth saying a couple of things.

x86's ___arch__swab64 is wrong.  Well - it was mostly ok for 32bit builds of Xen, and is not ok for 64bit builds.  As a consequence, this series nets an improvement of:

$ ../scripts/bloat-o-meter xen-syms-before xen-syms-after
add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-54 (-54)
Function                                     old     new   delta
elf_access_unsigned                          173     151     -22
unlzo                                       1128    1096     -32
Total: Before=3803328, After=3803274, chg -0.00%

because the code generation for bswap64 goes from:

ffff82d0402059b0 <_bswap64>:
ffff82d0402059b0:    48 89 f8                 mov    %rdi,%rax ffff82d0402059b3:    48 c1 e8 20              shr    $0x20,%rax ffff82d0402059b7:    0f cf                    bswap  %edi ffff82d0402059b9:    0f c8                    bswap  %eax ffff82d0402059bb:    97                       xchg   %eax,%edi ffff82d0402059bc:    48 89 c2                 mov    %rax,%rdx ffff82d0402059bf:    89 f8                    mov    %edi,%eax ffff82d0402059c1:    48 c1 e2 20              shl    $0x20,%rdx ffff82d0402059c5:    48 09 d0                 or     %rdx,%rax ffff82d0402059c8:    c3                       retq

to

ffff82d0402059b0 <_bswap64>:
ffff82d0402059b0:    48 89 f8                 mov    %rdi,%rax ffff82d0402059b3:    48 0f c8                 bswap  %rax ffff82d0402059b6:    c3                       retq

Almost all byteswapping is done on 32bit quantities, not 64, which is why the delta is so small.

However, it also drops 500 lines of code, which is a demonstration of how silly the swab() infrastructure was.  It also removes the need for per-arch code to do any of this.

I'd say its safe to go into 4.16, but I'll understand if others want to push back on that at this point in the release.

~Andrew

#2 lore raw

Re: [PATCH v2 1/7] xen: implement byteswap.h

In reply to Lin Liu

xen/include/xen/compiler.h line 182

Quoted 9 lines · Lin Liu
xen/include/xen/compiler.h +1 -0
index 696c7eb89e..68f28082a5 100644
@@ -179,4 +179,16 @@
 # define CLANG_DISABLE_WARN_GCC_COMPAT_END
 #endif
 
+#if (!defined(__clang__) && (__GNUC__ < 10))

This too lost my feedback.

It needs to be #ifndef __has_builtin because otherwise a random GCC 10 build I have fails with:

/local/security/xen.git/xen/include/xen/byteswap.h:6:19: error: missing binary operator before token "("
    6 | #if !__has_builtin(__builtin_bswap16)

I suspect it was a build of GCC 10 before __has_builtin() support was added, but either way, we should be predicating on __has_builtin itself, not version guesswork.

~Andrew

#9 lore raw

Re: [PATCH v2 5/7] xen/xsm: Switch to byteswap.h

In reply to Lin Liu
Quoted 61 lines · Lin Liu

Update to use byteswap.h to swap bytes

No functional change

---
Cc: Daniel De Graaf <[email protected]>
Cc: "Daniel P. Smith" <[email protected]>

---
 xen/xsm/flask/ss/avtab.c       | 2 +-
 xen/xsm/flask/ss/conditional.c | 2 +-
 xen/xsm/flask/ss/ebitmap.c     | 2 +-
 xen/xsm/flask/ss/policydb.c    | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
xen/xsm/flask/ss/avtab.c +1 -1
index bfc91c8b0c..1fa796f625 100644
@@ -19,8 +19,8 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
+#include <xen/byteswap.h>
 #include <xen/lib.h>
-#include <asm/byteorder.h>
 #include <xen/types.h>
 #include <xen/xmalloc.h>
 #include <xen/errno.h>
xen/xsm/flask/ss/conditional.c +1 -1
index 3e58aea551..059f6e07e5 100644
@@ -9,7 +9,7 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/types.h>
 #include <xen/errno.h>
xen/xsm/flask/ss/ebitmap.c +1 -1
index e1d0a586a7..1550437c6f 100644
@@ -10,7 +10,7 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/xmalloc.h>
 #include <xen/errno.h>
xen/xsm/flask/ss/policydb.c +0 -0
index 9426164353..595005c3b7 100644

xen/xsm/flask/ss/policydb.c line 28

Quoted 8 lines
xen/xsm/flask/ss/policydb.c +1 -1
@@ -22,7 +22,7 @@
 
 /* Ported to Xen 3.0, George Coker, <[email protected]> */
 
-#include <asm/byteorder.h>
+#include <xen/byteswap.h>
 #include <xen/lib.h>
 #include <xen/types.h>
 #include <xen/xmalloc.h>

Reviewed by: Daniel P. Smith <[email protected]>

#3 lore raw

Re: [PATCH v2 1/7] xen: implement byteswap.h

In reply to Lin Liu
Quoted 34 lines · Lin Liu
xen/include/xen/byteswap.h new file +31 -0
@@ -0,0 +1,93 @@
+#ifndef _BYTESWAP_H
+#define _BYTESWAP_H
+
+#include <xen/types.h>
+
+#if !__has_builtin(__builtin_bswap16)
+static always_inline uint16_t __builtin_bswap16(uint16_t val)
+{
+    return ((val & 0x00FF) << 8) | ((val & 0xFF00) >> 8);
+}
+#endif
+
+#if !__has_builtin(__builtin_bswap32)
+static always_inline uint32_t __builtin_bswap32(uint32_t val)
+{
+    return ((val & 0x000000FF) << 24) |
+           ((val & 0x0000FF00) <<  8) |
+           ((val & 0x00FF0000) >>  8) |
+           ((val & 0xFF000000) >> 24);
+}
+#endif
+
+#if !__has_builtin(__builtin_bswap64)
+static always_inline uint64_t __builtin_bswap64(uint64_t val)
+{
+    return ((val & 0x00000000000000FF) << 56) |
+           ((val & 0x000000000000FF00) << 40) |
+           ((val & 0x0000000000FF0000) << 24) |
+           ((val & 0x00000000FF000000) <<  8) |
+           ((val & 0x000000FF00000000) >>  8) |
+           ((val & 0x0000FF0000000000) >> 24) |

xen/include/xen/byteswap.h line 41

Quoted 10 lines
xen/include/xen/byteswap.h new file +10 -0
+           ((val & 0x00FF000000000000) >> 40) |
+           ((val & 0xFF00000000000000) >> 56);
+}
+#endif
+
+#define bswap16(x) __builtin_bswap16(x)
+#define bswap32(x) __builtin_bswap32(x)
+#define bswap64(x) __builtin_bswap64(x)
+
+#define bswap_ul(x) bswap##BITS_PER_LONG(x)

I don't see how this is supposed to work - the compiler isn't going to expand BITS_PER_LONG before the token concatenation. You'll need helper macros to achieve that. Linux has __PASTE(); we may want to "steal" that (albeit preferably without any leading underscores).

xen/include/xen/byteswap.h line 43

Quoted 1 line
xen/include/xen/byteswap.h new file +1 -0
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

While you've worked towards abstracting older vs newer compiler versions, I'm afraid these constants aren't available in gcc 4.1 yet. They look to have appeared in 4.6.

xen/include/xen/byteswap.h line 51

Quoted 7 lines
xen/include/xen/byteswap.h new file +7 -0
+#  ifndef __LITTLE_ENDIAN
+#    define __LITTLE_ENDIAN 1234
+#  endif
+
+#  ifndef __LITTLE_ENDIAN_BITFIELD
+#    define __LITTLE_ENDIAN_BITFIELD
+#  endif

These are definitions which I don't think belong into a header of this name. They're imo well placed in byteorder/*.h.

Jan

#13 lore raw

Re: [PATCH v2 6/7] xen: Switch to byteswap.h

In reply to Lin Liu

xen/common/xz/private.h static inline void put_unaligned_le32(u32 val, void *p) · line 31

Quoted 6 lines · Lin Liu
xen/common/xz/private.h +1 -1
@@ -28,7 +28,7 @@ static inline void put_unaligned_le32(u32 val, void *p)
 
 #endif
 
-#define get_le32(p) le32_to_cpup((const uint32_t *)(p))
+#define get_le32(p) le32_to_cpu(*(const uint32_t *)(p))

While here the adjustment may be okay as there is already an open-coded cast, ...

xen/include/xen/unaligned.h line 23

Quoted 6 lines
xen/include/xen/unaligned.h +1 -1
@@ -20,7 +20,7 @@
 
 static inline uint16_t get_unaligned_be16(const void *p)
 {
-	return be16_to_cpup(p);
+	return be16_to_cpu(*(uint16_t*)p);

... here and below you add open-coded casts. Is there a reason you don't retain the ..._to_cpup() macros?

Jan