Skip to contentSkip to conversation

make endian-independent unaligned memory access functions available to libhw

PATCH Superseded Paolo Bonzini 4 participants 15 messages Jun 6, 2011 – Jul 6, 2011 Kbuild

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover make endian-independent unaligned memory access functions available to libhw

Functions like ldl_be_p and ldl_le_p are currently used only as building blocks for {ld,st}XX_p. As such, they are in cpu-all.h even though they have absolutely no dependency on the target.

In order to make them globally available, this series moves them to bswap.h instead.

An interesting part of this is that there are functions also for floating point values. Leaving them in cpu-all.h would be possible but untidy. In fact handling these is easy, but it requires to make softfloat.h target-dependent as well. This is what patch 2 does.

v1->v2:
rebase, use softfloat-specialize.h instead of introducing softfloat-target.h

Paolo Bonzini (3):
move WORDS_ALIGNED to qemu-common.h
softfloat: change default nan definitions to variables move unaligned memory access functions to bswap.h

 Makefile.hw                |    2 +-
 bswap.h                    |  474 ++++++++++++++++++++++++++++++++++++++++++++
 configure                  |    3 +-
 cpu-all.h                  |  446 +-----------------------------------------
 cpu-common.h               |    4 -
 fpu/softfloat-specialize.h |   72 +++++++
 fpu/softfloat.h            |   60 +-----
 qemu-common.h              |    4 +
 8 files changed, 562 insertions(+), 503 deletions(-)
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH v2 1/3 move WORDS_ALIGNED to qemu-common.h

This is not a CPU interface.

---
 cpu-common.h  |    4 ----
 qemu-common.h |    4 ++++
 2 files changed, 4 insertions(+), 4 deletions(-)
cpu-common.h +0 -4
index 7a86d63..7979a18 100644
@@ -3,10 +3,6 @@
 
 /* CPU interfaces that are target indpendent.  */
 
-#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
-#define WORDS_ALIGNED
-#endif
-
 #ifdef TARGET_PHYS_ADDR_BITS
 #include "targphys.h"
 #endif
qemu-common.h +4 -0
index b851b20..7484ef8 100644
@@ -4,6 +4,10 @@
 
 #include "config-host.h"
 
+#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
+#define WORDS_ALIGNED
+#endif
+
 #define QEMU_NORETURN __attribute__ ((__noreturn__))
 #ifdef CONFIG_GCC_ATTRIBUTE_WARN_UNUSED_RESULT
 #define QEMU_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
Signature · 1 line
1.7.4.4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] move WORDS_ALIGNED to qemu-common.h

Am 06.06.2011 um 16:25 schrieb Paolo Bonzini:

Quoted 9 lines

This is not a CPU interface.

qemu-common.h +0 -0
index b851b20..7484ef8 100644

qemu-common.h line 10

Quoted 7 lines
qemu-common.h +3 -0
@@ -4,6 +4,10 @@
 
 #include "config-host.h"
 
+#if defined(__arm__) || defined(__sparc__) || defined(__mips__) ||  
 defined(__hppa__) || defined(__ia64__)
+#define WORDS_ALIGNED
+#endif

Since it depends on the host and you're placing it directly under config-host.h inclusion, might it make sense to move the decision into configure instead, so that it ends up in config-host.h?

Andreas

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] move WORDS_ALIGNED to qemu-common.h

Quoted 14 lines · Andreas Färber

Am 06.06.2011 um 16:25 schrieb Paolo Bonzini:

Quoted 12 lines

This is not a CPU interface.

qemu-common.h +0 -0
index b851b20..7484ef8 100644
@@ -4,6 +4,10 @@
 
 #include "config-host.h"
 
Quoted 8 lines

qemu-common.h line 9

Quoted 4 lines
qemu-common.h +3 -0
+#if defined(__arm__) || defined(__sparc__) || defined(__mips__) ||
 defined(__hppa__) || defined(__ia64__)
+#define WORDS_ALIGNED
+#endif

Since it depends on the host and you're placing it directly under config-host.h inclusion, might it make sense to move the decision into configure instead, so that it ends up in config-host.h?

Richard's suggestion will get rid of this altogether.

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] move WORDS_ALIGNED to qemu-common.h

In reply to Andreas Färber
Quoted 8 lines · Andreas Färber

Am 06.06.2011 um 16:25 schrieb Paolo Bonzini:

qemu-common.h line 9

Quoted 3 lines
qemu-common.h +3 -0
+#if defined(__arm__) || defined(__sparc__) || defined(__mips__) || defined(__hppa__) || defined(__ia64__)
+#define WORDS_ALIGNED
+#endif

Since it depends on the host and you're placing it directly under config-host.h inclusion, might it make sense to move the decision into configure instead, so that it ends up in config-host.h?

Hum, I now understand what Paulo was talking about elsewhere in the thread.

If he takes my suggestion to re-write the unaligned functions with GCC packed support, these host ifdefs go away, and this objection disappears. The question becomes one of ordering.

Do we take his existing 3-part patch as-is, and the packed patch as a followup? Do we convert to packed accesses first and move it around after? Do we do it all in one step?

r~

#5 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] move WORDS_ALIGNED to qemu-common.h

Quoted 3 lines · Richard Henderson

Do we take his existing 3-part patch as-is, and the packed patch as a followup? Do we convert to packed accesses first and move it around after? Do we do it all in one step?

Either of the first two works for me.

However, since this series was a start towards fixing real bugs reported by Coverity:

    qemu-kvm-0.14.0/hw/scsi-bus.c:190:
    sign_extension: Suspicious implicit sign extension:

    "cmd[10]" with type "unsigned char" (8 bits, unsigned) is promoted
    in "cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24)"
    to type "int" (32 bits, signed), then sign-extended to type
    "unsigned long" (64 bits, unsigned).

    If "cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24)"
    is greater than 0x7FFFFFFF, the upper bits of the result will all
    be 1.

... and there were objections on requiring recent GCC, perhaps it's better to just commit it as is.

Paolo

#6 lore raw

PATCH v2 2/3 softfloat: change default nan definitions to variables

In reply to Paolo Bonzini

Most definitions in softfloat.h are really target-independent, but the file is not because it includes definitions of the default NaN values. Change those to variables to allow including softfloat.h from files that are not compiled per-target. By making them const, the compiler is allowed to optimize them into softfloat functions that use them.

---
 fpu/softfloat-specialize.h |   72 ++++++++++++++++++++++++++++++++++++++++++++
 fpu/softfloat.h            |   60 +++++-------------------------------
 2 files changed, 81 insertions(+), 51 deletions(-)
fpu/softfloat-specialize.h +72 -0
index c7d35a1..c165205 100644
@@ -35,6 +35,78 @@ these four paragraphs for those parts of this code that are retained.
 
 =============================================================================*/
 
+#if defined(TARGET_MIPS) || defined(TARGET_SH4) || defined(TARGET_UNICORE32)
+#define SNAN_BIT_IS_ONE		1
+#else
+#define SNAN_BIT_IS_ONE		0
+#endif
+
+/*----------------------------------------------------------------------------
+| The pattern for a default generated half-precision NaN.
+*----------------------------------------------------------------------------*/
+#if defined(TARGET_ARM)
+const float16 float16_default_nan = const_float16(0x7E00);
+#elif SNAN_BIT_IS_ONE
+const float16 float16_default_nan = const_float16(0x7DFF);
+#else
+const float16 float16_default_nan = const_float16(0xFE00);
+#endif
+
+/*----------------------------------------------------------------------------
+| The pattern for a default generated single-precision NaN.
+*----------------------------------------------------------------------------*/
+#if defined(TARGET_SPARC)
+const float32 float32_default_nan = const_float32(0x7FFFFFFF);
+#elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA)
+const float32 float32_default_nan = const_float32(0x7FC00000);
+#elif SNAN_BIT_IS_ONE
+const float32 float32_default_nan = const_float32(0x7FBFFFFF);
+#else
+const float32 float32_default_nan = const_float32(0xFFC00000);
+#endif
+
+/*----------------------------------------------------------------------------
+| The pattern for a default generated double-precision NaN.
+*----------------------------------------------------------------------------*/
+#if defined(TARGET_SPARC)
+const float64 float64_default_nan = const_float64(LIT64( 0x7FFFFFFFFFFFFFFF ));
+#elif defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_ALPHA)
+const float64 float64_default_nan = const_float64(LIT64( 0x7FF8000000000000 ));
+#elif SNAN_BIT_IS_ONE
+const float64 float64_default_nan = const_float64(LIT64( 0x7FF7FFFFFFFFFFFF ));
+#else
+const float64 float64_default_nan = const_float64(LIT64( 0xFFF8000000000000 ));
+#endif
+
+/*----------------------------------------------------------------------------
+| The pattern for a default generated extended double-precision NaN.
+*----------------------------------------------------------------------------*/
+#if SNAN_BIT_IS_ONE
+#define floatx80_default_nan_high 0x7FFF
+#define floatx80_default_nan_low  LIT64( 0xBFFFFFFFFFFFFFFF )
+#else
+#define floatx80_default_nan_high 0xFFFF
+#define floatx80_default_nan_low  LIT64( 0xC000000000000000 )
+#endif
+
+const floatx80 floatx80_default_nan = make_floatx80(floatx80_default_nan_high,
+                                                    floatx80_default_nan_low);
+
+/*----------------------------------------------------------------------------
+| The pattern for a default generated quadruple-precision NaN.  The `high' and
+| `low' values hold the most- and least-significant bits, respectively.
+*----------------------------------------------------------------------------*/
+#if SNAN_BIT_IS_ONE
+#define float128_default_nan_high LIT64( 0x7FFF7FFFFFFFFFFF )
+#define float128_default_nan_low  LIT64( 0xFFFFFFFFFFFFFFFF )
+#else
+#define float128_default_nan_high LIT64( 0xFFFF800000000000 )
+#define float128_default_nan_low  LIT64( 0x0000000000000000 )
+#endif
+
+const float128 float128_default_nan = make_float128(float128_default_nan_high,
+                                                    float128_default_nan_low);
+
 /*----------------------------------------------------------------------------
 | Raises the exceptions specified by `flags'.  Floating-point traps can be
 | defined here if desired.  It is currently not possible for such a trap
fpu/softfloat.h +9 -51

109 lines not on this page · raw message

Signature · 1 line
1.7.4.4
#7 lore raw

PATCH v2 3/3 move unaligned memory access functions to bswap.h

In reply to Paolo Bonzini

This is just code movement, and moving the fpu/ include path from target-dependent to target-independent Make variables.

---
 Makefile.hw |    2 +-
 bswap.h     |  474 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure   |    3 +-
 cpu-all.h   |  446 +-------------------------------------------------------
 4 files changed, 477 insertions(+), 448 deletions(-)
Makefile.hw +1 -1
index b9181ab..659e441 100644
@@ -9,7 +9,7 @@ include $(SRC_PATH)/rules.mak
 
 $(call set-vpath, $(SRC_PATH):$(SRC_PATH)/hw)
 
-QEMU_CFLAGS+=-I.. -I$(SRC_PATH)/fpu
+QEMU_CFLAGS+=-I..
 
 include $(SRC_PATH)/Makefile.objs
 
bswap.h +474 -0

484 lines not on this page · raw message

configure +1 -2
index d38b952..29c5bcc 100755
@@ -233,7 +233,7 @@ QEMU_CFLAGS="-Wall -Wundef -Wwrite-strings -Wmissing-prototypes $QEMU_CFLAGS"
 QEMU_CFLAGS="-Wstrict-prototypes -Wredundant-decls $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE $QEMU_CFLAGS"
 QEMU_CFLAGS="-D_FORTIFY_SOURCE=2 $QEMU_CFLAGS"
-QEMU_INCLUDES="-I. -I\$(SRC_PATH)"
+QEMU_INCLUDES="-I. -I\$(SRC_PATH) -I\$(SRC_PATH)/fpu"
 LDFLAGS="-g $LDFLAGS"
 
 # make source path absolute
@@ -3415,7 +3415,6 @@ else
   includes="-I\$(SRC_PATH)/tcg/\$(ARCH) $includes"
 fi
 includes="-I\$(SRC_PATH)/tcg $includes"
-includes="-I\$(SRC_PATH)/fpu $includes"
 
 if test "$target_user_only" = "yes" ; then
     libdis_config_mak=libdis-user/config.mak
cpu-all.h +1 -445

465 lines not on this page · raw message

Signature · 1 line
1.7.4.4
Reviewed-by on v2 3/3
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] move unaligned memory access functions to bswap.h

Patches 1-3:

That said,

bswap.h static inline uint32_t qemu_bswap_len(uint32_t value, int len) · line 360

Quoted 12 lines · Paolo Bonzini
bswap.h +12 -0
+/* conservative code for little endian unaligned accesses */
+static inline int lduw_le_p(const void *ptr)
+{
+#ifdef _ARCH_PPC
+    int val;
+    __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
+    return val;
+#else
+    const uint8_t *p = ptr;
+    return p[0] | (p[1] << 8);
+#endif
+}

Can we add a patch 4/3 that removes this sort of hard-coded assembly stuff in favour of generic gcc code. E.g.

static inline int lduw_le_p(const void *ptr) {
struct pack { uint16_t x __attribute__((packed)); }; const struct pack *p = ptr;
uint16_t ret;

  ret = p->x;
  ret = le_bswap(ret, 16);

return ret;
}

One could fairly well macroize all 6 instances.

The compiler knows that ppc and i386 can do the unaligned loads. The compiler *should* be able to match the bswap_N patterns, and thus also fold the unaligned load with the bswap for ppc.

I can confirm that gcc head does in fact do the right thing for ppc32/64 here, as well as for i386/x86_64. I don't have previous versions of gcc checked out atm...

I havn't checked, but this *ought* to enable the load-asi bswap instruction for sparcv9. I also havn't checked what happens for a target like sparcv8 that lacks both unaligned load and bswap, to see that we don't simply double the number of shifts. However, I'd be tempted to file that as a gcc missed optimization bug.

r~

#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] move unaligned memory access functions to bswap.h

Quoted 2 lines · Richard Henderson

Can we add a patch 4/3 that removes this sort of hard-coded assembly stuff in favour of generic gcc code.

Then it's also possible to commit patch 2 from this submission, and wait while I rework the other two to use generic GCC code.

Paolo

#10 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] move unaligned memory access functions to bswap.h

Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Richard Henderson

Can we add a patch 4/3 that removes this sort of hard-coded assembly stuff in favour of generic gcc code.

Then it's also possible to commit patch 2 from this submission, and wait while I rework the other two to use generic GCC code.

I began the message by acking all of 1 through 3.

The re-work for GCC code ought to be a follow-on patch.

r~

#11 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] move unaligned memory access functions to bswap.h

In reply to Richard Henderson
Quoted 36 lines · Richard Henderson

Patches 1-3:

That said,

bswap.h static inline uint32_t qemu_bswap_len(uint32_t value, int len) · line 360

Quoted 12 lines · Paolo Bonzini
bswap.h +12 -0
+/* conservative code for little endian unaligned accesses */
+static inline int lduw_le_p(const void *ptr)
+{
+#ifdef _ARCH_PPC
+    int val;
+    __asm__ __volatile__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (ptr));
+    return val;
+#else
+    const uint8_t *p = ptr;
+    return p[0] | (p[1] << 8);
+#endif
+}

Can we add a patch 4/3 that removes this sort of hard-coded assembly stuff in favour of generic gcc code. E.g.

static inline int lduw_le_p(const void *ptr) {
struct pack { uint16_t x __attribute__((packed)); }; const struct pack *p = ptr;
uint16_t ret;

  ret = p->x;
  ret = le_bswap(ret, 16);

return ret;
}

One could fairly well macroize all 6 instances.

Quoted 7 lines

The compiler knows that ppc and i386 can do the unaligned loads. The compiler *should* be able to match the bswap_N patterns, and thus also fold the unaligned load with the bswap for ppc.

I can confirm that gcc head does in fact do the right thing for ppc32/64 here, as well as for i386/x86_64. I don't have previous versions of gcc checked out atm...

Depends on how bswap_16 is defined. If it is __builtin_bswap16 then 4.5.0 and 4.6.0 generate byte reversed loads, and previous versions lack that builtin, so i don't think this generic code should go in.

Quoted 10 lines

I havn't checked, but this *ought* to enable the load-asi bswap instruction for sparcv9. I also havn't checked what happens for a target like sparcv8 that lacks both unaligned load and bswap, to see that we don't simply double the number of shifts. However, I'd be tempted to file that as a gcc missed optimization bug.

r~

Signature · 1 line
mailto:[email protected]
#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] move unaligned memory access functions to bswap.h

Quoted 4 lines · malc

Depends on how bswap_16 is defined. If it is __builtin_bswap16 then 4.5.0 and 4.6.0 generate byte reversed loads, and previous versions lack that builtin, so i don't think this generic code should go in.

It would continue to be defined as-is, without direct reference to the __builtin_bswap functions. But you're right that the generic code would depend on the bswap optimization pass that recognizes the mask/shift/or pattern and converts it to the builtins internally.

What if we kept the ppc ifdefs, but converted the rest to the gcc generic code?

r~

#13 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] move unaligned memory access functions to bswap.h

Quoted 6 lines · Richard Henderson
Quoted 4 lines · malc

Depends on how bswap_16 is defined. If it is __builtin_bswap16 then 4.5.0 and 4.6.0 generate byte reversed loads, and previous versions lack that builtin, so i don't think this generic code should go in.

Quoted 9 lines

It would continue to be defined as-is, without direct reference to the __builtin_bswap functions. But you're right that the generic code would depend on the bswap optimization pass that recognizes the mask/shift/or pattern and converts it to the builtins internally.

What if we kept the ppc ifdefs, but converted the rest to the gcc generic code?

No objections.

Signature · 1 line
mailto:[email protected]
#14 lore raw
In reply to Paolo Bonzini
Quoted 21 lines · Paolo Bonzini

Functions like ldl_be_p and ldl_le_p are currently used only as building blocks for {ld,st}XX_p. As such, they are in cpu-all.h even though they have absolutely no dependency on the target.

In order to make them globally available, this series moves them to bswap.h instead.

An interesting part of this is that there are functions also for floating point values. Leaving them in cpu-all.h would be possible but untidy. In fact handling these is easy, but it requires to make softfloat.h target-dependent as well. This is what patch 2 does.

v1->v2:
rebase, use softfloat-specialize.h instead of introducing softfloat-target.h

Paolo Bonzini (3):
move WORDS_ALIGNED to qemu-common.h
softfloat: change default nan definitions to variables move unaligned memory access functions to bswap.h

Quoted 10 lines
  Makefile.hw                |    2 +-
  bswap.h                    |  474 ++++++++++++++++++++++++++++++++++++++++++++
  configure                  |    3 +-
  cpu-all.h                  |  446 +-----------------------------------------
  cpu-common.h               |    4 -
  fpu/softfloat-specialize.h |   72 +++++++
  fpu/softfloat.h            |   60 +-----
  qemu-common.h              |    4 +
  8 files changed, 562 insertions(+), 503 deletions(-)

Ping?

Paolo