Re: [PATCH] x86: do not expose CONFIG_BSWAP to userspace

From: Arnd Bergmann
Date: Tue Feb 03 2009 - 13:21:00 EST


On Thursday 29 January 2009, H. Peter Anvin wrote:
> Harvey Harrison wrote:
> > On Wed, 2009-01-28 at 15:27 -0800, H. Peter Anvin wrote:
> >> Harvey Harrison wrote:
> >>> Well, that's unfortunate, how about we just export the BSWAP version
> >>> unconditionally and hope pure i386 just goes away someday?
> >>>
> >> Well, we already have MOVBE coming up, too...
> >>
> >
> > Is someone already working on an __arch_swab{16|32|64}p to use them?
> >
>
> Not that I know of, but it's trivial enough.  They can also be used for
> all-register swapping, too, with the advantage that you get register
> decoupling.

I just realized that gcc-4.3 and higher have the __builtin_bswap{16,32,64}
functions on x86, which are supposed to do the right thing on any
platform. Maybe a patch like the one below can solve this for both the
kernel and for other users of the byteorder headers. Unfortunately, I
could not find out whether the builtins are available on all other
platforms as well.

Signed-off-by: Arnd Bergmann <arnd@xxxxxxxx>

--- a/arch/x86/include/asm/swab.h
+++ b/arch/x86/include/asm/swab.h
@@ -4,9 +4,17 @@
#include <linux/types.h>
#include <linux/compiler.h>

+#ifdef __CHECKER__
+extern unsigned long __builtin_bswap_32(unsigned long x);
+extern unsigned long long __builtin_bswap_64(unsigned long long x);
+#endif
+
static inline __attribute_const__ __u32 __arch_swab32(__u32 val)
{
-#ifdef __i386__
+#if defined(__GNUC__) && ((__GNUC__ > 4) || \
+ ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
+ val = __builtin_bswap32(val);
+#elif defined (__i386__)
# ifdef CONFIG_X86_BSWAP
asm("bswap %0" : "=r" (val) : "0" (val));
# else
@@ -28,7 +36,10 @@ static inline __attribute_const__ __u32 __arch_swab32(__u32 val)

static inline __attribute_const__ __u64 __arch_swab64(__u64 val)
{
-#ifdef __i386__
+#if defined(__GNUC__) && ((__GNUC__ > 4) || \
+ ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
+ return __builtin_bswap64(val);
+#elif defined (__i386__)
union {
struct {
__u32 a;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/