xen: have a more generic unaligned.h header
v3 cover xen: have a more generic unaligned.h header
Update Xen's unaligned.h header to support all architectures, allowing to remove the architecture specific variants (x86 only until now).
Changes in V2:
- new patch 1 (Julien Grall)
- adjusted patch 2 (Jan Beulich)
Changes in V3:
- adjusted patch 2 (Andrew Cooper)
Juergen Gross (3):
xen/arm: set -mno-unaligned-access compiler option for Arm32
xen: make include/xen/unaligned.h usable on all architectures
xen: remove asm/unaligned.h
xen/arch/arm/arch.mk | 1 + xen/arch/x86/include/asm/unaligned.h | 6 ---- xen/common/lz4/defs.h | 2 +- xen/common/lzo.c | 2 +- xen/common/unlzo.c | 2 +- xen/common/xz/private.h | 2 +- xen/common/zstd/mem.h | 2 +- xen/include/xen/unaligned.h | 53 ++++++++++++++++------------ xen/lib/xxhash32.c | 2 +- xen/lib/xxhash64.c | 2 +- 10 files changed, 38 insertions(+), 36 deletions(-) delete mode 100644 xen/arch/x86/include/asm/unaligned.h
Signature · 1 line
2.35.3
PATCH v3 1/3 xen/arm: set -mno-unaligned-access compiler option for Arm32
As the hypervisor is disabling unaligned accesses for Arm32, set the -mno-unaligned-access compiler option for building. This will prohibit unaligned accesses when e.g. accessing 2- or 4-byte data items in packed data structures.
Backport: 4.15+
---
V2:
- new patch
--- xen/arch/arm/arch.mk | 1 + 1 file changed, 1 insertion(+)
xen/arch/arm/arch.mk +1 -0
@@ -7,6 +7,7 @@ $(call cc-option-add,CFLAGS,CC,-Wnested-externs) # Prevent floating-point variables from creeping into Xen. CFLAGS-$(CONFIG_ARM_32) += -msoft-float CFLAGS-$(CONFIG_ARM_32) += -mcpu=cortex-a15 +CFLAGS-$(CONFIG_ARM_32) += -mno-unaligned-access CFLAGS-$(CONFIG_ARM_64) += -mcpu=generic CFLAGS-$(CONFIG_ARM_64) += -mgeneral-regs-only # No fp registers etc
Signature · 1 line
2.35.3
PATCH v3 2/3 xen: make include/xen/unaligned.h usable on all architectures
In reply to Juergen GrossInstead of defining get_unaligned() and put_unaligned() in a way that is only supporting architectures allowing unaligned accesses, use the same approach as the Linux kernel and let the compiler do the decision how to generate the code for probably unaligned data accesses.
Update include/xen/unaligned.h from include/asm-generic/unaligned.h of the Linux kernel.
The generated code has been checked to be the same on x86.
Modify the Linux variant to not use underscore prefixed identifiers, avoid unneeded parentheses and drop the 24-bit accessors.
Origin: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 803f4e1eab7a
---
V2:
- drop 24-bit accessors (Jan Beulich)
- avoid underscore prefixed identifiers (Jan Beulich)
- drop unneeded parentheses (Jan Beulich)
V3:
- drop trailing underscores in names (Andrew Cooper)
- keep existing function sequence and types (Andrew Cooper)
--- xen/include/xen/unaligned.h | 53 +++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 23 deletions(-)
xen/include/xen/unaligned.h +30 -23
@@ -1,12 +1,4 @@ -/* -* This header can be used by architectures where unaligned accesses work -* without faulting, and at least reasonably efficiently. Other architectures -* will need to have a custom asm/unaligned.h. -*/ -#ifndef __ASM_UNALIGNED_H__ -#error "xen/unaligned.h should not be included directly - include asm/unaligned.h instead" -#endif - +/* SPDX-License-Identifier: GPL-2.0 */ #ifndef __XEN_UNALIGNED_H__#define __XEN_UNALIGNED_H__
@@ -15,67 +7,82 @@#include <asm/byteorder.h> #endif -#define get_unaligned (p) (*(p)) -#define put_unaligned (val, p) (*(p) = (val)) +/* +* This is the most generic implementation of unaligned accesses +* and should work almost anywhere. +*/ + +#define get_unaligned_t (type, ptr) ({ \ + const struct { type x; } __packed *ptr_ = (typeof(ptr_))(ptr); \ + ptr_->x; \ +}) + +#define put_unaligned_t (type, val, ptr) do { \ + struct { type x; } __packed *ptr_ = (typeof(ptr_))(ptr); \ + ptr_->x = val; \ +} while (0) + +#define get_unaligned (ptr) get_unaligned_t(typeof(*(ptr)), ptr) +#define put_unaligned (val, ptr) put_unaligned_t(typeof(*(ptr)), val, ptr)static inline uint16_t get_unaligned_be16 (const void *p) { -return be16_to_cpup (p); +return be16_to_cpu (get_unaligned_t (__be16, p)); }static inline void put_unaligned_be16 (uint16_t val,void *p) { - *(__force __be16 *)p =cpu_to_be16 (val); +put_unaligned_t (__be16,cpu_to_be16 (val), p); }static inline uint32_t get_unaligned_be32 (const void *p) { -return be32_to_cpup (p); +return be32_to_cpu (get_unaligned_t (__be32, p)); }static inline void put_unaligned_be32 (uint32_t val,void *p) { - *(__force __be32 *)p =cpu_to_be32 (val); +put_unaligned_t (__be32,cpu_to_be32 (val), p); }static inline uint64_t get_unaligned_be64 (const void *p) { -return be64_to_cpup (p); +return be64_to_cpu (get_unaligned_t (__be64, p)); }static inline void put_unaligned_be64 (uint64_t val,void *p) { - *(__force __be64 *)p =cpu_to_be64 (val); +put_unaligned_t (__be64,cpu_to_be64 (val), p); }static inline uint16_t get_unaligned_le16 (const void *p) { -return le16_to_cpup (p); +return le16_to_cpu (get_unaligned_t (__le16, p)); }static inline void put_unaligned_le16 (uint16_t val,void *p) { - *(__force __le16 *)p =cpu_to_le16 (val); +put_unaligned_t (__le16,cpu_to_le16 (val), p); }static inline uint32_t get_unaligned_le32 (const void *p) { -return le32_to_cpup (p); +return le32_to_cpu (get_unaligned_t (__le32, p)); }static inline void put_unaligned_le32 (uint32_t val,void *p) { - *(__force __le32 *)p =cpu_to_le32 (val); +put_unaligned_t (__le32,cpu_to_le32 (val), p); }static inline uint64_t get_unaligned_le64 (const void *p) { -return le64_to_cpup (p); +return le64_to_cpu (get_unaligned_t (__le64, p)); }static inline void put_unaligned_le64 (uint64_t val,void *p) { - *(__force __le64 *)p =cpu_to_le64 (val); +put_unaligned_t (__le64,cpu_to_le64 (val), p); }#endif /* __XEN_UNALIGNED_H__ */
Signature · 1 line
2.35.3
PATCH v3 3/3 xen: remove asm/unaligned.h
In reply to Juergen GrossWith include/xen/unaligned.h now dealing properly with unaligned accesses for all architectures, asm/unaligned.h can be removed and users can be switched to include xen/unaligned.h instead.
--- xen/arch/x86/include/asm/unaligned.h | 6 ------ xen/common/lz4/defs.h | 2 +- xen/common/lzo.c | 2 +- xen/common/unlzo.c | 2 +- xen/common/xz/private.h | 2 +- xen/common/zstd/mem.h | 2 +- xen/lib/xxhash32.c | 2 +- xen/lib/xxhash64.c | 2 +- 8 files changed, 7 insertions(+), 13 deletions(-) delete mode 100644 xen/arch/x86/include/asm/unaligned.h
xen/arch/x86/include/asm/unaligned.h deleted +0 -6
@@ -1,6 +0,0 @@ -#ifndef __ASM_UNALIGNED_H__ -#define __ASM_UNALIGNED_H__ - -#include <xen/unaligned.h> - -#endif /* __ASM_UNALIGNED_H__ */
xen/common/lz4/defs.h +1 -1
@@ -10,7 +10,7 @@#ifdef __XEN__#include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else static inline u16 get_unaligned_le16 (const void *p)
xen/common/lzo.c +1 -1
@@ -97,7 +97,7 @@#ifdef __XEN__#include <xen/lib.h> #include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else #define get_unaligned_le16 (_p) (*(u16 *)(_p))#endif
xen/common/unlzo.c +1 -1
@@ -34,7 +34,7 @@#ifdef __XEN__#include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else static inline u16 get_unaligned_be16 (const void *p)
xen/common/xz/private.h +1 -1
@@ -13,7 +13,7 @@#ifdef __XEN__#include <xen/kernel.h> #include <asm/byteorder.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #else static inline u32 get_unaligned_le32 (const void *p)
xen/common/zstd/mem.h +1 -1
@@ -23,7 +23,7 @@#ifdef __XEN__#include <xen/string.h> /* memcpy */ #include <xen/types.h> /* size_t, ptrdiff_t */ -#include <asm/unaligned.h> +#include <xen/unaligned.h> #endif /*-****************************************
xen/lib/xxhash32.c +1 -1
@@ -42,7 +42,7 @@#include <xen/errno.h> #include <xen/string.h> #include <xen/xxhash.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> /*-************************************* * Macros
xen/lib/xxhash64.c +1 -1
@@ -43,7 +43,7 @@#include <xen/errno.h> #include <xen/string.h> #include <xen/xxhash.h> -#include <asm/unaligned.h> +#include <xen/unaligned.h> #endif /*-*************************************
Signature · 1 line
2.35.3
Hi Juergen,
Quoted 7 lines · Juergen Gross
Update Xen's
unaligned.hheader to support all architectures, allowing to remove the architecture specific variants (x86 only until now).Changes in V2:
- new patch 1 (Julien Grall)
- adjusted patch 2 (Jan Beulich)
Quoted 7 lines
Changes in V3:
- adjusted patch 2 (Andrew Cooper)Juergen Gross (3):
xen/arm: set -mno-unaligned-access compiler option for Arm32
xen: makeinclude/xen/unaligned.husable on all architectures
xen: removeasm/unaligned.h
The series is now committed.
Cheers,
Signature · 1 line
Julien Grall
Quoted 13 lines · Julien Grall
Hi Juergen,
Quoted 10 lines · Juergen Gross
Update Xen's
unaligned.hheader to support all architectures, allowing to remove the architecture specific variants (x86 only until now).Changes in V2:
- new patch 1 (Julien Grall)
- adjusted patch 2 (Jan Beulich)Changes in V3:
- adjusted patch 2 (Andrew Cooper)
Quoted 6 lines
Quoted 4 lines
Juergen Gross (3):
xen/arm: set -mno-unaligned-access compiler option for Arm32
xen: makeinclude/xen/unaligned.husable on all architectures
xen: removeasm/unaligned.hThe series is now committed.
Either patches #2 or #3 broke gitlab CI:
https://gitlab.com/xen-project/hardware/xen/-/jobs/5733143618
I have reverted both of them to unblock it.
Cheers,
Signature · 1 line
Julien Grall