Skip to contentSkip to conversation

system/memory: Clarify unaligned load/store API

PATCH Superseded Philippe Mathieu-Daudé 4 participants 12 messages Dec 28, 2025 – Dec 30, 2025 Networking

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover system/memory: Clarify unaligned load/store API

I have been confused by the load/store 'host endian' API for years: this is just about unaligned loads/stores. Rename to clarify.

Philippe Mathieu-Daudé (3):
system/memory: Remove ldn_he_p() and stn_he_p() helpers system/memory: Extract 'qemu/memory_ldst_unaligned.h' header system/memory: Rename unaligned load/store API

 include/qemu/bswap.h                 | 95 +++++-----------------------
 include/qemu/memory_ldst_unaligned.h | 67 ++++++++++++++++++++
 accel/tcg/translator.c               |  7 +-
 hw/display/ati_2d.c                  |  3 +-
 hw/display/sm501.c                   | 21 +++---
 hw/remote/vfio-user-obj.c            |  5 +-
 hw/vmapple/virtio-blk.c              |  3 +-
 net/checksum.c                       |  7 +-
 system/memory.c                      |  4 +-
 system/physmem.c                     |  8 +--
 ui/vnc-enc-tight.c                   |  3 +-
 util/bufferiszero.c                  |  8 +--
 accel/tcg/ldst_atomicity.c.inc       | 10 +--
 13 files changed, 128 insertions(+), 113 deletions(-)
 create mode 100644 include/qemu/memory_ldst_unaligned.h
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/3 system/memory: Remove ldn_he_p() and stn_he_p() helpers

ldn_he_p() and stn_he_p() helpers expand to a plain memcpy(), but in a convoluted way. We are not using them, just remove so we are not tempted to start to.

---
 include/qemu/bswap.h | 1 -
 1 file changed, 1 deletion(-)
include/qemu/bswap.h +0 -1
index 65a1b3634f4..b77ea955de5 100644
@@ -412,7 +412,6 @@ static inline void stq_be_p(void *ptr, uint64_t v)
         }                                                               \
     }
 
-DO_STN_LDN_P(he)
 DO_STN_LDN_P(le)
 DO_STN_LDN_P(be)
 
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH 1/3] system/memory: Remove ldn_he_p() and stn_he_p() helpers

Il dom 28 dic 2025, 17:18 Philippe Mathieu-Daudé <[email protected]> ha scritto:

Quoted 4 lines

ldn_he_p() and stn_he_p() helpers expand to a plain memcpy(), but in a convoluted way. We are not using them, just remove so we are not tempted to start to.

I am confused, doesn't ati_2d.c use them? And in the future they could also be used by ram_device_ops etc.

Paolo

Quoted 20 lines
---
 include/qemu/bswap.h | 1 -
 1 file changed, 1 deletion(-)
include/qemu/bswap.h +0 -2
index 65a1b3634f4..b77ea955de5 100644
@@ -412,7 +412,6 @@ static inline void stq_be_p(void *ptr, uint64_t v)
         }                                                               \
     }
 
-DO_STN_LDN_P(he)
 DO_STN_LDN_P(le)
 DO_STN_LDN_P(be)
 
--

2.52.0

#3 lore raw

Re: [PATCH 1/3] system/memory: Remove ldn_he_p() and stn_he_p() helpers

Quoted 10 lines · Paolo Bonzini

Il dom 28 dic 2025, 17:18 Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> ha scritto:

ldn_he_p() and stn_he_p() helpers expand to a plain memcpy(), but in a convoluted way. We are not using them, just remove so we are not tempted to start to.

I am confused, doesn't ati_2d.c use them? And in the future they could also be used by ram_device_ops etc.

Yeah sorry, this patch is crap, I should better get some rest.

#4 lore raw

PATCH 2/3 system/memory: Extract 'qemu/memory_ldst_unaligned.h' header

In reply to Philippe Mathieu-Daudé

Unaligned memcpy API is buried within 'qemu/bswap.h', supposed to be related to endianness swapping. Extract to a new header to clarify.

---
 include/qemu/bswap.h                 | 62 +------------------------
 include/qemu/memory_ldst_unaligned.h | 67 ++++++++++++++++++++++++++++
 accel/tcg/translator.c               |  1 +
 hw/display/ati_2d.c                  |  1 +
 hw/display/sm501.c                   |  2 +-
 hw/remote/vfio-user-obj.c            |  1 +
 hw/vmapple/virtio-blk.c              |  1 +
 net/checksum.c                       |  1 +
 ui/vnc-enc-tight.c                   |  1 +
 util/bufferiszero.c                  |  2 +-
 10 files changed, 76 insertions(+), 63 deletions(-)
 create mode 100644 include/qemu/memory_ldst_unaligned.h
include/qemu/bswap.h +1 -61
index b77ea955de5..e8b944988c3 100644
@@ -1,6 +1,7 @@
 #ifndef BSWAP_H
 #define BSWAP_H
 
+#include "qemu/memory_ldst_unaligned.h"
 #include "qemu/target-info.h"
 
 #undef  bswap16
@@ -173,8 +174,6 @@ CPU_CONVERT(le, 64, uint64_t)
 # define const_le16(_x) (_x)
 #endif
 
-/* unaligned/endian-independent pointer access */
-
 /*
  * the generic syntax is:
  *
@@ -201,7 +200,6 @@ CPU_CONVERT(le, 64, uint64_t)
  *   q: 64 bits
  *
  * endian is:
- *   he   : host endian
  *   be   : big endian
  *   le   : little endian
  *   te   : target endian
@@ -237,64 +235,6 @@ static inline void stb_p(void *ptr, uint8_t v)
     *(uint8_t *)ptr = v;
 }
 
-/*
- * Any compiler worth its salt will turn these memcpy into native unaligned
- * operations.  Thus we don't need to play games with packed attributes, or
- * inline byte-by-byte stores.
- * Some compilation environments (eg some fortify-source implementations)
- * may intercept memcpy() in a way that defeats the compiler optimization,
- * though, so we use __builtin_memcpy() to give ourselves the best chance
- * of good performance.
- */
-
-static inline int lduw_he_p(const void *ptr)
-{
-    uint16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline int ldsw_he_p(const void *ptr)
-{
-    int16_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline void stw_he_p(void *ptr, uint16_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
-
-static inline void st24_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, 3);
-}
-
-static inline int ldl_he_p(const void *ptr)
-{
-    int32_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline void stl_he_p(void *ptr, uint32_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
-
-static inline uint64_t ldq_he_p(const void *ptr)
-{
-    uint64_t r;
-    __builtin_memcpy(&r, ptr, sizeof(r));
-    return r;
-}
-
-static inline void stq_he_p(void *ptr, uint64_t v)
-{
-    __builtin_memcpy(ptr, &v, sizeof(v));
-}
-
 static inline int lduw_le_p(const void *ptr)
 {
     return (uint16_t)le_bswap(lduw_he_p(ptr), 16);
include/qemu/memory_ldst_unaligned.h new file +67 -0

67 lines not on this page · raw message

accel/tcg/translator.c +1 -0
index 034f2f359ef..86cdd70c47f 100644
@@ -9,6 +9,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/bswap.h"
+#include "qemu/memory_ldst_unaligned.h"
 #include "qemu/log.h"
 #include "qemu/error-report.h"
 #include "accel/tcg/cpu-ldst-common.h"
hw/display/ati_2d.c +1 -0
index 309bb5ccb6c..24a3c3e942f 100644
@@ -10,6 +10,7 @@
 #include "qemu/osdep.h"
 #include "ati_int.h"
 #include "ati_regs.h"
+#include "qemu/memory_ldst_unaligned.h"
 #include "qemu/log.h"
 #include "ui/pixel_ops.h"
 #include "ui/console.h"
hw/display/sm501.c +1 -1
index bc091b3c9fb..51efe2ad41f 100644
@@ -28,6 +28,7 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/log.h"
+#include "qemu/memory_ldst_unaligned.h"
 #include "qemu/module.h"
 #include "hw/usb/hcd-ohci.h"
 #include "hw/char/serial-mm.h"
@@ -40,7 +41,6 @@
 #include "hw/display/i2c-ddc.h"
 #include "qemu/range.h"
 #include "ui/pixel_ops.h"
-#include "qemu/bswap.h"
 #include "trace.h"
 #include "qom/object.h"
 
hw/remote/vfio-user-obj.c +1 -0
index 216b4876e24..9c9ac8b0d92 100644
@@ -49,6 +49,7 @@
 #include "qapi/error.h"
 #include "qapi/qapi-visit-sockets.h"
 #include "qapi/qapi-events-misc.h"
+#include "qemu/memory_ldst_unaligned.h"
 #include "qemu/notify.h"
 #include "qemu/thread.h"
 #include "qemu/main-loop.h"
hw/vmapple/virtio-blk.c +1 -0
index 9de9aaae0bf..f5e8e92df75 100644
@@ -19,6 +19,7 @@
 #include "hw/vmapple/vmapple.h"
 #include "hw/virtio/virtio-blk.h"
 #include "hw/virtio/virtio-pci.h"
+#include "qemu/memory_ldst_unaligned.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
 #include "qapi/error.h"
net/checksum.c +1 -0
index 537457d89d0..18be31c26e5 100644
@@ -16,6 +16,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/memory_ldst_unaligned.h"
 #include "net/checksum.h"
 #include "net/eth.h"
 
ui/vnc-enc-tight.c +1 -0
index 9dfe6ae5a24..b645aebccef 100644
@@ -42,6 +42,7 @@
 #include <jpeglib.h>
 #endif
 
+#include "qemu/memory_ldst_unaligned.h"
 #include "qemu/bswap.h"
 #include "vnc.h"
 #include "vnc-enc-tight.h"
util/bufferiszero.c +1 -1
index 522146dab97..9548dd3ad1b 100644
@@ -23,7 +23,7 @@
  */
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
-#include "qemu/bswap.h"
+#include "qemu/memory_ldst_unaligned.h"
 #include "host/cpuinfo.h"
 
 typedef bool (*biz_accel_fn)(const void *, size_t);
Signature · 1 line
2.52.0
#5 lore raw

Re: [PATCH 2/3] system/memory: Extract 'qemu/memory_ldst_unaligned.h' header

Quoted 25 lines · Philippe Mathieu-Daudé

Unaligned memcpy API is buried within 'qemu/bswap.h', supposed to be related to endianness swapping. Extract to a new header to clarify.

---

include/qemu/bswap.h                 | 62 +------------------------
include/qemu/memory_ldst_unaligned.h | 67 ++++++++++++++++++++++++++++
accel/tcg/translator.c               |  1 +
hw/display/ati_2d.c                  |  1 +
hw/display/sm501.c                   |  2 +-
hw/remote/vfio-user-obj.c            |  1 +
hw/vmapple/virtio-blk.c              |  1 +
net/checksum.c                       |  1 +
ui/vnc-enc-tight.c                   |  1 +
util/bufferiszero.c                  |  2 +-
10 files changed, 76 insertions(+), 63 deletions(-)

create mode 100644 include/qemu/memory_ldst_unaligned.h

include/qemu/bswap.h +0 -0
index b77ea955de5..e8b944988c3 100644

include/qemu/bswap.h line 4

Quoted 4 lines
include/qemu/bswap.h +1 -0
@@ -1,6 +1,7 @@
 #ifndef BSWAP_H
 #define BSWAP_H
 
+#include "qemu/memory_ldst_unaligned.h"

If it's included here do users need to also include it separately or if so should some of those users lose bswap.h include now instead of including both this header and bswap.h? I think it's simpler to only include it here and let users get it through bswap.h unless you review and remove now unnecessary bswap.h includes from places that only need this hearder but I don't know if that's worth the effort.

Regards,
BALATON Zoltan

#6 lore raw

Re: [PATCH 2/3] system/memory: Extract 'qemu/memory_ldst_unaligned.h' header

Hi Zoltan,

Quoted 29 lines · BALATON Zoltan
Quoted 28 lines · Philippe Mathieu-Daudé

Unaligned memcpy API is buried within 'qemu/bswap.h', supposed to be related to endianness swapping. Extract to a new header to clarify.

---
include/qemu/bswap.h                 | 62 +------------------------ include/qemu/memory_ldst_unaligned.h | 67 ++++++++++++++++++++++++++++ accel/tcg/translator.c               |  1 + hw/display/ati_2d.c                  |  1 + hw/display/sm501.c                   |  2 +- hw/remote/vfio-user-obj.c            |  1 + hw/vmapple/virtio-blk.c              |  1 +
net/checksum.c                       |  1 + ui/vnc-enc-tight.c                   |  1 + util/bufferiszero.c                  |  2 +-

10 files changed, 76 insertions(+), 63 deletions(-)

create mode 100644 include/qemu/memory_ldst_unaligned.h

include/qemu/bswap.h +0 -0
index b77ea955de5..e8b944988c3 100644
@@ -1,6 +1,7 @@
 #ifndef BSWAP_H
 #define BSWAP_H
 
Quoted 3 lines

include/qemu/bswap.h line 4

Quoted 1 line
include/qemu/bswap.h +1 -0
+#include "qemu/memory_ldst_unaligned.h"

If it's included here do users need to also include it separately or if so should some of those users lose bswap.h include now instead of including both this header and bswap.h? I think it's simpler to only include it here and let users get it through bswap.h unless you review and remove now unnecessary bswap.h includes from places that only need this hearder but I don't know if that's worth the effort.

bswap API users have to include <qemu/bswap.h>,
users of ld/st_unaligned() one have to include <qemu/memory_ldst_unaligned.h>.

This is a one to one match, what is the problem?

#7 lore raw

Re: [PATCH 2/3] system/memory: Extract 'qemu/memory_ldst_unaligned.h' header

Quoted 32 lines · Philippe Mathieu-Daudé

Hi Zoltan,

Quoted 29 lines · BALATON Zoltan
Quoted 28 lines · Philippe Mathieu-Daudé

Unaligned memcpy API is buried within 'qemu/bswap.h', supposed to be related to endianness swapping. Extract to a new header to clarify.

---
include/qemu/bswap.h                 | 62 +------------------------ include/qemu/memory_ldst_unaligned.h | 67 ++++++++++++++++++++++++++++ accel/tcg/translator.c               |  1 + hw/display/ati_2d.c                  |  1 + hw/display/sm501.c                   |  2 +- hw/remote/vfio-user-obj.c            |  1 + hw/vmapple/virtio-blk.c              |  1 +
net/checksum.c                       |  1 + ui/vnc-enc-tight.c                   |  1 + util/bufferiszero.c                  |  2 +-

10 files changed, 76 insertions(+), 63 deletions(-)

create mode 100644 include/qemu/memory_ldst_unaligned.h

include/qemu/bswap.h +0 -0
index b77ea955de5..e8b944988c3 100644
@@ -1,6 +1,7 @@
 #ifndef BSWAP_H
 #define BSWAP_H
 
Quoted 6 lines
Quoted 3 lines

include/qemu/bswap.h line 4

Quoted 1 line
include/qemu/bswap.h +1 -0
+#include "qemu/memory_ldst_unaligned.h"

If it's included here do users need to also include it separately or if so should some of those users lose bswap.h include now instead of including both this header and bswap.h? I think it's simpler to only include it here and let users get it through bswap.h unless you review and remove now unnecessary bswap.h includes from places that only need this hearder but I don't know if that's worth the effort.

bswap API users have to include <qemu/bswap.h>,
users of ld/st_unaligned() one have to include <qemu/ memory_ldst_unaligned.h>.

Let me try again.

Users of the ld/st_unaligned() API have to include <qemu/ldst_unaligned.h>; if they don't use <qemu/bswap.h> then first it is pointless to include it, but furthermore it saves few compilation cycles for the unused inlined bswap functions.

Users of the bswap() API have to include <qemu/bswap.h> (which indirectly includes <qemu/ldst_unaligned.h>, but this is irrelevant).

Users of both the bswap() and ld/st_unaligned() APIs have to include both <qemu/bswap.h> AND <qemu/ldst_unaligned.h>, even if it is indirectly included. That makes the use of APIs more explicit in the source file, and furthermore it avoids code churn when indirect headers are reworked.

For recent examples see commits 34bcd8f4ff9 ("hw/int/loongarch:
Include missing 'system/memory.h' header") or fdda97b47e6 ("hw/arm/virt-acpi-build: Include missing 'cpu.h' header").

Is it clearer?

Regards,

Phil.

#8 lore raw

Re: [PATCH 2/3] system/memory: Extract 'qemu/memory_ldst_unaligned.h' header

Quoted 52 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Philippe Mathieu-Daudé

Hi Zoltan,

Quoted 29 lines · BALATON Zoltan
Quoted 28 lines · Philippe Mathieu-Daudé

Unaligned memcpy API is buried within 'qemu/bswap.h', supposed to be related to endianness swapping. Extract to a new header to clarify.

---
include/qemu/bswap.h                 | 62 +------------------------ include/qemu/memory_ldst_unaligned.h | 67 ++++++++++++++++++++++++++++ accel/tcg/translator.c               |  1 + hw/display/ati_2d.c                  |  1 + hw/display/sm501.c                   |  2 +- hw/remote/vfio-user-obj.c            |  1 + hw/vmapple/virtio-blk.c              |  1 +
net/checksum.c                       |  1 + ui/vnc-enc-tight.c                   |  1 + util/bufferiszero.c                  |  2 +-

10 files changed, 76 insertions(+), 63 deletions(-)

create mode 100644 include/qemu/memory_ldst_unaligned.h

include/qemu/bswap.h +0 -0
index b77ea955de5..e8b944988c3 100644
@@ -1,6 +1,7 @@
 #ifndef BSWAP_H
 #define BSWAP_H
 
Quoted 6 lines
Quoted 3 lines

include/qemu/bswap.h line 4

Quoted 1 line
include/qemu/bswap.h +1 -0
+#include "qemu/memory_ldst_unaligned.h"

If it's included here do users need to also include it separately or if so should some of those users lose bswap.h include now instead of including both this header and bswap.h? I think it's simpler to only include it here and let users get it through bswap.h unless you review and remove now unnecessary bswap.h includes from places that only need this hearder but I don't know if that's worth the effort.

bswap API users have to include <qemu/bswap.h>,
users of ld/st_unaligned() one have to include <qemu/ memory_ldst_unaligned.h>.

Let me try again.

Users of the ld/st_unaligned() API have to include <qemu/ldst_unaligned.h>; if they don't use <qemu/bswap.h> then first it is pointless to include it, but furthermore it saves few compilation cycles for the unused inlined bswap functions.

Users of the bswap() API have to include <qemu/bswap.h> (which indirectly includes <qemu/ldst_unaligned.h>, but this is irrelevant).

Quoted 5 lines

Users of both the bswap() and ld/st_unaligned() APIs have to include both <qemu/bswap.h> AND <qemu/ldst_unaligned.h>, even if it is indirectly included. That makes the use of APIs more explicit in the source file, and furthermore it avoids code churn when indirect headers are reworked.

Your patch added #include of the new header to 8 files so this is already causing churn in the hope of avoiding it.

Quoted 5 lines

For recent examples see commits 34bcd8f4ff9 ("hw/int/loongarch:
Include missing 'system/memory.h' header") or fdda97b47e6 ("hw/arm/virt-acpi-build: Include missing 'cpu.h' header").

Is it clearer?

I did not notice that you removed bswap.h from 2 files and thought the patch only added the new header. In that case my comment does not apply but I still see this more as a churn than useful cleanup. OK, host endian does not swap but if you consider that the le and be functions in bswap.h are based on the he functions then you can just leave these there. If it bothers you add a comment, that's still less churn.

Regards,
BALATON Zoltan

#9 lore raw

PATCH 3/3 system/memory: Rename unaligned load/store API

In reply to Philippe Mathieu-Daudé

Rename the API methods using the explicit 'unaligned' description instead of 'he' which stands for 'host endianness'.

---
 include/qemu/bswap.h                 | 32 ++++++++++++++--------------
 include/qemu/memory_ldst_unaligned.h | 16 +++++++-------
 accel/tcg/translator.c               |  6 +++---
 hw/display/ati_2d.c                  |  2 +-
 hw/display/sm501.c                   | 19 ++++++++++-------
 hw/remote/vfio-user-obj.c            |  4 ++--
 hw/vmapple/virtio-blk.c              |  2 +-
 net/checksum.c                       |  6 +++---
 system/memory.c                      |  4 ++--
 system/physmem.c                     |  8 +++----
 ui/vnc-enc-tight.c                   |  2 +-
 util/bufferiszero.c                  |  6 +++---
 accel/tcg/ldst_atomicity.c.inc       | 10 ++++-----
 13 files changed, 60 insertions(+), 57 deletions(-)
include/qemu/bswap.h +16 -16
index e8b944988c3..f7c806085fb 100644
@@ -237,82 +237,82 @@ static inline void stb_p(void *ptr, uint8_t v)
 
 static inline int lduw_le_p(const void *ptr)
 {
-    return (uint16_t)le_bswap(lduw_he_p(ptr), 16);
+    return (uint16_t)le_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldsw_le_p(const void *ptr)
 {
-    return (int16_t)le_bswap(lduw_he_p(ptr), 16);
+    return (int16_t)le_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldl_le_p(const void *ptr)
 {
-    return le_bswap(ldl_he_p(ptr), 32);
+    return le_bswap(ldl_unaligned_p(ptr), 32);
 }
 
 static inline uint64_t ldq_le_p(const void *ptr)
 {
-    return le_bswap(ldq_he_p(ptr), 64);
+    return le_bswap(ldq_unaligned_p(ptr), 64);
 }
 
 static inline void stw_le_p(void *ptr, uint16_t v)
 {
-    stw_he_p(ptr, le_bswap(v, 16));
+    stw_unaligned_p(ptr, le_bswap(v, 16));
 }
 
 static inline void st24_le_p(void *ptr, uint32_t v)
 {
-    st24_he_p(ptr, le_bswap24(v));
+    st24_unaligned_p(ptr, le_bswap24(v));
 }
 
 static inline void stl_le_p(void *ptr, uint32_t v)
 {
-    stl_he_p(ptr, le_bswap(v, 32));
+    stl_unaligned_p(ptr, le_bswap(v, 32));
 }
 
 static inline void stq_le_p(void *ptr, uint64_t v)
 {
-    stq_he_p(ptr, le_bswap(v, 64));
+    stq_unaligned_p(ptr, le_bswap(v, 64));
 }
 
 static inline int lduw_be_p(const void *ptr)
 {
-    return (uint16_t)be_bswap(lduw_he_p(ptr), 16);
+    return (uint16_t)be_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldsw_be_p(const void *ptr)
 {
-    return (int16_t)be_bswap(lduw_he_p(ptr), 16);
+    return (int16_t)be_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldl_be_p(const void *ptr)
 {
-    return be_bswap(ldl_he_p(ptr), 32);
+    return be_bswap(ldl_unaligned_p(ptr), 32);
 }
 
 static inline uint64_t ldq_be_p(const void *ptr)
 {
-    return be_bswap(ldq_he_p(ptr), 64);
+    return be_bswap(ldq_unaligned_p(ptr), 64);
 }
 
 static inline void stw_be_p(void *ptr, uint16_t v)
 {
-    stw_he_p(ptr, be_bswap(v, 16));
+    stw_unaligned_p(ptr, be_bswap(v, 16));
 }
 
 static inline void st24_be_p(void *ptr, uint32_t v)
 {
-    st24_he_p(ptr, be_bswap24(v));
+    st24_unaligned_p(ptr, be_bswap24(v));
 }
 
 static inline void stl_be_p(void *ptr, uint32_t v)
 {
-    stl_he_p(ptr, be_bswap(v, 32));
+    stl_unaligned_p(ptr, be_bswap(v, 32));
 }
 
 static inline void stq_be_p(void *ptr, uint64_t v)
 {
-    stq_he_p(ptr, be_bswap(v, 64));
+    stq_unaligned_p(ptr, be_bswap(v, 64));
 }
 
 /* Store v to p as a sz byte value in host order */
include/qemu/memory_ldst_unaligned.h +8 -8
index f6b64e8fe9c..6b0cc3e2777 100644
@@ -16,50 +16,50 @@
  * of good performance.
  */
 
-static inline int lduw_he_p(const void *ptr)
+static inline int lduw_unaligned_p(const void *ptr)
 {
     uint16_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline int ldsw_he_p(const void *ptr)
+static inline int ldsw_unaligned_p(const void *ptr)
 {
     int16_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline void stw_he_p(void *ptr, uint16_t v)
+static inline void stw_unaligned_p(void *ptr, uint16_t v)
 {
     __builtin_memcpy(ptr, &v, sizeof(v));
 }
 
-static inline void st24_he_p(void *ptr, uint32_t v)
+static inline void st24_unaligned_p(void *ptr, uint32_t v)
 {
     __builtin_memcpy(ptr, &v, 3);
 }
 
-static inline int ldl_he_p(const void *ptr)
+static inline int ldl_unaligned_p(const void *ptr)
 {
     int32_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline void stl_he_p(void *ptr, uint32_t v)
+static inline void stl_unaligned_p(void *ptr, uint32_t v)
 {
     __builtin_memcpy(ptr, &v, sizeof(v));
 }
 
-static inline uint64_t ldq_he_p(const void *ptr)
+static inline uint64_t ldq_unaligned_p(const void *ptr)
 {
     uint64_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline void stq_he_p(void *ptr, uint64_t v)
+static inline void stq_unaligned_p(void *ptr, uint64_t v)
 {
     __builtin_memcpy(ptr, &v, sizeof(v));
 }
accel/tcg/translator.c +3 -3
index 86cdd70c47f..497128c96c5 100644
@@ -342,14 +342,14 @@ static bool translator_ld(CPUArchState *env, DisasContextBase *db,
     case 2:
         if (QEMU_IS_ALIGNED(pc, 2)) {
             uint16_t t = qatomic_read((uint16_t *)host);
-            stw_he_p(dest, t);
+            stw_unaligned_p(dest, t);
             return true;
         }
         break;
     case 4:
         if (QEMU_IS_ALIGNED(pc, 4)) {
             uint32_t t = qatomic_read((uint32_t *)host);
-            stl_he_p(dest, t);
+            stl_unaligned_p(dest, t);
             return true;
         }
         break;
@@ -357,7 +357,7 @@ static bool translator_ld(CPUArchState *env, DisasContextBase *db,
     case 8:
         if (QEMU_IS_ALIGNED(pc, 8)) {
             uint64_t t = qatomic_read__nocheck((uint64_t *)host);
-            stq_he_p(dest, t);
+            stq_unaligned_p(dest, t);
             return true;
         }
         break;
hw/display/ati_2d.c +1 -1
index 24a3c3e942f..333d78d7455 100644
@@ -222,7 +222,7 @@ void ati_2d_blt(ATIVGAState *s)
             for (y = 0; y < s->regs.dst_height; y++) {
                 i = dst_x * bypp + (dst_y + y) * dst_pitch;
                 for (x = 0; x < s->regs.dst_width; x++, i += bypp) {
-                    stn_he_p(&dst_bits[i], bypp, filler);
+                    stn_unaligned_p(&dst_bits[i], bypp, filler);
                 }
             }
         }
hw/display/sm501.c +11 -8
index 51efe2ad41f..776fedebb13 100644
@@ -768,7 +768,7 @@ static void sm501_2d_operation(SM501State *s)
             for (y = 0; y < height; y++) {
                 i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                 for (x = 0; x < width; x++, i += bypp) {
-                    stn_he_p(&d[i], bypp, ~ldn_he_p(&d[i], bypp));
+                    stn_unaligned_p(&d[i], bypp, ~ldn_unaligned_p(&d[i], bypp));
                 }
             }
         } else if (!rop_mode && rop == 0x99) {
@@ -781,8 +781,9 @@ static void sm501_2d_operation(SM501State *s)
                 i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                 j = (src_x + (src_y + y) * src_pitch) * bypp;
                 for (x = 0; x < width; x++, i += bypp, j += bypp) {
-                    stn_he_p(&d[i], bypp,
-                             ~(ldn_he_p(&sp[j], bypp) ^ ldn_he_p(&d[i], bypp)));
+                    stn_unaligned_p(&d[i], bypp,
+                                    ~(ldn_unaligned_p(&sp[j], bypp)
+                                      ^ ldn_unaligned_p(&d[i], bypp)));
                 }
             }
         } else if (!rop_mode && rop == 0xee) {
@@ -795,8 +796,9 @@ static void sm501_2d_operation(SM501State *s)
                 i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                 j = (src_x + (src_y + y) * src_pitch) * bypp;
                 for (x = 0; x < width; x++, i += bypp, j += bypp) {
-                    stn_he_p(&d[i], bypp,
-                             ldn_he_p(&sp[j], bypp) | ldn_he_p(&d[i], bypp));
+                    stn_unaligned_p(&d[i], bypp,
+                                    ldn_unaligned_p(&sp[j], bypp)
+                                    | ldn_unaligned_p(&d[i], bypp));
                 }
             }
         } else {
@@ -818,8 +820,9 @@ static void sm501_2d_operation(SM501State *s)
             if (width == 1 && height == 1) {
                 unsigned int si = (src_x + src_y * src_pitch) * bypp;
                 unsigned int di = (dst_x + dst_y * dst_pitch) * bypp;
-                stn_he_p(&s->local_mem[dst_base + di], bypp,
-                         ldn_he_p(&s->local_mem[src_base + si], bypp));
+                stn_unaligned_p(&s->local_mem[dst_base + di], bypp,
+                                ldn_unaligned_p(&s->local_mem[src_base + si],
+                                                bypp));
                 break;
             }
             /* If reverse blit do simple check for overlaps */
@@ -917,7 +920,7 @@ static void sm501_2d_operation(SM501State *s)
                 for (y = 0; y < height; y++) {
                     i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                     for (x = 0; x < width; x++, i += bypp) {
-                        stn_he_p(&d[i], bypp, color);
+                        stn_unaligned_p(&d[i], bypp, color);
                     }
                 }
             }
hw/remote/vfio-user-obj.c +2 -2
index 9c9ac8b0d92..44adff10eb9 100644
@@ -392,7 +392,7 @@ static int vfu_object_mr_rw(MemoryRegion *mr, uint8_t *buf, hwaddr offset,
         access_size = memory_access_size(mr, size, offset);
 
         if (is_write) {
-            val = ldn_he_p(ptr, access_size);
+            val = ldn_unaligned_p(ptr, access_size);
 
             result = memory_region_dispatch_write(mr, offset, val,
                                                   size_memop(access_size),
@@ -402,7 +402,7 @@ static int vfu_object_mr_rw(MemoryRegion *mr, uint8_t *buf, hwaddr offset,
                                                  size_memop(access_size),
                                                  MEMTXATTRS_UNSPECIFIED);
 
-            stn_he_p(ptr, access_size, val);
+            stn_unaligned_p(ptr, access_size, val);
         }
 
         if (release_lock) {
hw/vmapple/virtio-blk.c +1 -1
index f5e8e92df75..805bcbc9467 100644
@@ -79,7 +79,7 @@ static void vmapple_virtio_blk_get_config(VirtIODevice *vdev, uint8_t *config)
     g_assert(dev->parent_obj.config_size >= endof(struct virtio_blk_config, zoned));
 
     /* Apple abuses the field for max_secure_erase_sectors as type id */
-    stl_he_p(&blkcfg->max_secure_erase_sectors, dev->apple_type);
+    stl_unaligned_p(&blkcfg->max_secure_erase_sectors, dev->apple_type);
 }
 
 static void vmapple_virtio_blk_class_init(ObjectClass *klass, const void *data)
net/checksum.c +3 -3
index 18be31c26e5..56314f5e76d 100644
@@ -110,7 +110,7 @@ void net_checksum_calculate(void *data, int length, int csum_flag)
 
     /* Calculate IP checksum */
     if (csum_flag & CSUM_IP) {
-        stw_he_p(&ip->ip_sum, 0);
+        stw_unaligned_p(&ip->ip_sum, 0);
         csum = net_raw_checksum((uint8_t *)ip, IP_HDR_GET_LEN(ip));
         stw_be_p(&ip->ip_sum, csum);
     }
@@ -142,7 +142,7 @@ void net_checksum_calculate(void *data, int length, int csum_flag)
         }
 
         /* Set csum to 0 */
-        stw_he_p(&tcp->th_sum, 0);
+        stw_unaligned_p(&tcp->th_sum, 0);
 
         csum = net_checksum_tcpudp(ip_len, ip->ip_p,
                                    (uint8_t *)&ip->ip_src,
@@ -166,7 +166,7 @@ void net_checksum_calculate(void *data, int length, int csum_flag)
         }
 
         /* Set csum to 0 */
-        stw_he_p(&udp->uh_sum, 0);
+        stw_unaligned_p(&udp->uh_sum, 0);
 
         csum = net_checksum_tcpudp(ip_len, ip->ip_p,
                                    (uint8_t *)&ip->ip_src,
system/memory.c +2 -2
index 8b84661ae36..653d46e8e79 100644
@@ -1365,7 +1365,7 @@ static uint64_t memory_region_ram_device_read(void *opaque,
                                               hwaddr addr, unsigned size)
 {
     MemoryRegion *mr = opaque;
-    uint64_t data = ldn_he_p(mr->ram_block->host + addr, size);
+    uint64_t data = ldn_unaligned_p(mr->ram_block->host + addr, size);
 
     trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data, size);
 
@@ -1379,7 +1379,7 @@ static void memory_region_ram_device_write(void *opaque, hwaddr addr,
 
     trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
 
-    stn_he_p(mr->ram_block->host + addr, size, data);
+    stn_unaligned_p(mr->ram_block->host + addr, size, data);
 }
 
 static const MemoryRegionOps ram_device_mem_ops = {
system/physmem.c +4 -4
index fb69cdb57d9..d62af6f2480 100644
@@ -3284,7 +3284,7 @@ static MemTxResult flatview_write_continue_step(MemTxAttrs attrs,
 
         /*
          * Assure Coverity (and ourselves) that we are not going to OVERRUN
-         * the buffer by following ldn_he_p().
+         * the buffer by following ldn_unaligned_p().
          */
 #ifdef QEMU_STATIC_ANALYSIS
         assert((*l == 1 && len >= 1) ||
@@ -3292,7 +3292,7 @@ static MemTxResult flatview_write_continue_step(MemTxAttrs attrs,
                (*l == 4 && len >= 4) ||
                (*l == 8 && len >= 8));
 #endif
-        val = ldn_he_p(buf, *l);
+        val = ldn_unaligned_p(buf, *l);
         result = memory_region_dispatch_write(mr, mr_addr, val,
                                               size_memop(*l), attrs);
         if (release_lock) {
@@ -3379,7 +3379,7 @@ static MemTxResult flatview_read_continue_step(MemTxAttrs attrs, uint8_t *buf,
 
         /*
          * Assure Coverity (and ourselves) that we are not going to OVERRUN
-         * the buffer by following stn_he_p().
+         * the buffer by following stn_unaligned_p().
          */
 #ifdef QEMU_STATIC_ANALYSIS
         assert((*l == 1 && len >= 1) ||
@@ -3387,7 +3387,7 @@ static MemTxResult flatview_read_continue_step(MemTxAttrs attrs, uint8_t *buf,
                (*l == 4 && len >= 4) ||
                (*l == 8 && len >= 8));
 #endif
-        stn_he_p(buf, *l, val);
+        stn_unaligned_p(buf, *l, val);
 
         if (release_lock) {
             bql_unlock();
ui/vnc-enc-tight.c +1 -1
index b645aebccef..62d825dc05f 100644
@@ -912,7 +912,7 @@ static void tight_pack24(VncState *vs, uint8_t *buf, size_t count, size_t *ret)
     }
 
     while (count--) {
-        pix = ldl_he_p(buf8);
+        pix = ldl_unaligned_p(buf8);
         *buf++ = (char)(pix >> rshift);
         *buf++ = (char)(pix >> gshift);
         *buf++ = (char)(pix >> bshift);
util/bufferiszero.c +3 -3
index 9548dd3ad1b..d7775dc891d 100644
@@ -38,10 +38,10 @@ static bool buffer_is_zero_int_lt256(const void *buf, size_t len)
      * the beginning and end of the buffer.
      */
     if (unlikely(len <= 8)) {
-        return (ldl_he_p(buf) | ldl_he_p(buf + len - 4)) == 0;
+        return (ldl_unaligned_p(buf) | ldl_unaligned_p(buf + len - 4)) == 0;
     }
 
-    t = ldq_he_p(buf) | ldq_he_p(buf + len - 8);
+    t = ldq_unaligned_p(buf) | ldq_unaligned_p(buf + len - 8);
     p = QEMU_ALIGN_PTR_DOWN(buf + 8, 8);
     e = QEMU_ALIGN_PTR_DOWN(buf + len - 1, 8);
 
@@ -58,7 +58,7 @@ static bool buffer_is_zero_int_ge256(const void *buf, size_t len)
      * Use unaligned memory access functions to handle
      * the beginning and end of the buffer.
      */
-    uint64_t t = ldq_he_p(buf) | ldq_he_p(buf + len - 8);
+    uint64_t t = ldq_unaligned_p(buf) | ldq_unaligned_p(buf + len - 8);
     const uint64_t *p = QEMU_ALIGN_PTR_DOWN(buf + 8, 8);
     const uint64_t *e = QEMU_ALIGN_PTR_DOWN(buf + len - 1, 8);
 
accel/tcg/ldst_atomicity.c.inc +5 -5
index c735add2615..eeb18c716c0 100644
@@ -415,7 +415,7 @@ static uint16_t load_atom_2(CPUState *cpu, uintptr_t ra,
     atmax = required_atomicity(cpu, pi, memop);
     switch (atmax) {
     case MO_8:
-        return lduw_he_p(pv);
+        return lduw_unaligned_p(pv);
     case MO_16:
         /* The only case remaining is MO_ATOM_WITHIN16. */
         if (!HAVE_al8_fast && (pi & 3) == 1) {
@@ -512,7 +512,7 @@ static uint64_t load_atom_8(CPUState *cpu, uintptr_t ra,
     }
     switch (atmax) {
     case MO_8:
-        return ldq_he_p(pv);
+        return ldq_unaligned_p(pv);
     case MO_16:
         return load_atom_8_by_2(pv);
     case MO_32:
@@ -875,7 +875,7 @@ static void store_atom_2(CPUState *cpu, uintptr_t ra,
 
     atmax = required_atomicity(cpu, pi, memop);
     if (atmax == MO_8) {
-        stw_he_p(pv, val);
+        stw_unaligned_p(pv, val);
         return;
     }
 
@@ -928,7 +928,7 @@ static void store_atom_4(CPUState *cpu, uintptr_t ra,
     atmax = required_atomicity(cpu, pi, memop);
     switch (atmax) {
     case MO_8:
-        stl_he_p(pv, val);
+        stl_unaligned_p(pv, val);
         return;
     case MO_16:
         store_atom_4_by_2(pv, val);
@@ -996,7 +996,7 @@ static void store_atom_8(CPUState *cpu, uintptr_t ra,
     atmax = required_atomicity(cpu, pi, memop);
     switch (atmax) {
     case MO_8:
-        stq_he_p(pv, val);
+        stq_unaligned_p(pv, val);
         return;
     case MO_16:
         store_atom_8_by_2(pv, val);
Signature · 1 line
2.52.0
#10 lore raw

Re: [PATCH 3/3] system/memory: Rename unaligned load/store API

Il dom 28 dic 2025, 17:19 Philippe Mathieu-Daudé <[email protected]> ha scritto:

Quoted 10 lines

Rename the API methods using the explicit 'unaligned' description instead of 'he' which stands for 'host endianness'.

---
 include/qemu/bswap.h                 | 32 ++++++++++++++--------------
 include/qemu/memory_ldst_unaligned.h | 16 +++++++-------

No "memory_" since it's for host pointers and not guest memory addresses.

Paolo

 accel/tcg/translator.c               |  6 +++---
Quoted 531 lines
 hw/display/ati_2d.c                  |  2 +-
 hw/display/sm501.c                   | 19 ++++++++++-------
 hw/remote/vfio-user-obj.c            |  4 ++--
 hw/vmapple/virtio-blk.c              |  2 +-
 net/checksum.c                       |  6 +++---
 system/memory.c                      |  4 ++--
 system/physmem.c                     |  8 +++----
 ui/vnc-enc-tight.c                   |  2 +-
 util/bufferiszero.c                  |  6 +++---
 accel/tcg/ldst_atomicity.c.inc       | 10 ++++-----
 13 files changed, 60 insertions(+), 57 deletions(-)
include/qemu/bswap.h +16 -16
index e8b944988c3..f7c806085fb 100644
@@ -237,82 +237,82 @@ static inline void stb_p(void *ptr, uint8_t v)
 
 static inline int lduw_le_p(const void *ptr)
 {
-    return (uint16_t)le_bswap(lduw_he_p(ptr), 16);
+    return (uint16_t)le_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldsw_le_p(const void *ptr)
 {
-    return (int16_t)le_bswap(lduw_he_p(ptr), 16);
+    return (int16_t)le_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldl_le_p(const void *ptr)
 {
-    return le_bswap(ldl_he_p(ptr), 32);
+    return le_bswap(ldl_unaligned_p(ptr), 32);
 }
 
 static inline uint64_t ldq_le_p(const void *ptr)
 {
-    return le_bswap(ldq_he_p(ptr), 64);
+    return le_bswap(ldq_unaligned_p(ptr), 64);
 }
 
 static inline void stw_le_p(void *ptr, uint16_t v)
 {
-    stw_he_p(ptr, le_bswap(v, 16));
+    stw_unaligned_p(ptr, le_bswap(v, 16));
 }
 
 static inline void st24_le_p(void *ptr, uint32_t v)
 {
-    st24_he_p(ptr, le_bswap24(v));
+    st24_unaligned_p(ptr, le_bswap24(v));
 }
 
 static inline void stl_le_p(void *ptr, uint32_t v)
 {
-    stl_he_p(ptr, le_bswap(v, 32));
+    stl_unaligned_p(ptr, le_bswap(v, 32));
 }
 
 static inline void stq_le_p(void *ptr, uint64_t v)
 {
-    stq_he_p(ptr, le_bswap(v, 64));
+    stq_unaligned_p(ptr, le_bswap(v, 64));
 }
 
 static inline int lduw_be_p(const void *ptr)
 {
-    return (uint16_t)be_bswap(lduw_he_p(ptr), 16);
+    return (uint16_t)be_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldsw_be_p(const void *ptr)
 {
-    return (int16_t)be_bswap(lduw_he_p(ptr), 16);
+    return (int16_t)be_bswap(lduw_unaligned_p(ptr), 16);
 }
 
 static inline int ldl_be_p(const void *ptr)
 {
-    return be_bswap(ldl_he_p(ptr), 32);
+    return be_bswap(ldl_unaligned_p(ptr), 32);
 }
 
 static inline uint64_t ldq_be_p(const void *ptr)
 {
-    return be_bswap(ldq_he_p(ptr), 64);
+    return be_bswap(ldq_unaligned_p(ptr), 64);
 }
 
 static inline void stw_be_p(void *ptr, uint16_t v)
 {
-    stw_he_p(ptr, be_bswap(v, 16));
+    stw_unaligned_p(ptr, be_bswap(v, 16));
 }
 
 static inline void st24_be_p(void *ptr, uint32_t v)
 {
-    st24_he_p(ptr, be_bswap24(v));
+    st24_unaligned_p(ptr, be_bswap24(v));
 }
 
 static inline void stl_be_p(void *ptr, uint32_t v)
 {
-    stl_he_p(ptr, be_bswap(v, 32));
+    stl_unaligned_p(ptr, be_bswap(v, 32));
 }
 
 static inline void stq_be_p(void *ptr, uint64_t v)
 {
-    stq_he_p(ptr, be_bswap(v, 64));
+    stq_unaligned_p(ptr, be_bswap(v, 64));
 }
 
 /* Store v to p as a sz byte value in host order */
include/qemu/memory_ldst_unaligned.h +8 -8
index f6b64e8fe9c..6b0cc3e2777 100644
@@ -16,50 +16,50 @@
  * of good performance.
  */
 
-static inline int lduw_he_p(const void *ptr)
+static inline int lduw_unaligned_p(const void *ptr)
 {
     uint16_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline int ldsw_he_p(const void *ptr)
+static inline int ldsw_unaligned_p(const void *ptr)
 {
     int16_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline void stw_he_p(void *ptr, uint16_t v)
+static inline void stw_unaligned_p(void *ptr, uint16_t v)
 {
     __builtin_memcpy(ptr, &v, sizeof(v));
 }
 
-static inline void st24_he_p(void *ptr, uint32_t v)
+static inline void st24_unaligned_p(void *ptr, uint32_t v)
 {
     __builtin_memcpy(ptr, &v, 3);
 }
 
-static inline int ldl_he_p(const void *ptr)
+static inline int ldl_unaligned_p(const void *ptr)
 {
     int32_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline void stl_he_p(void *ptr, uint32_t v)
+static inline void stl_unaligned_p(void *ptr, uint32_t v)
 {
     __builtin_memcpy(ptr, &v, sizeof(v));
 }
 
-static inline uint64_t ldq_he_p(const void *ptr)
+static inline uint64_t ldq_unaligned_p(const void *ptr)
 {
     uint64_t r;
     __builtin_memcpy(&r, ptr, sizeof(r));
     return r;
 }
 
-static inline void stq_he_p(void *ptr, uint64_t v)
+static inline void stq_unaligned_p(void *ptr, uint64_t v)
 {
     __builtin_memcpy(ptr, &v, sizeof(v));
 }
accel/tcg/translator.c +2 -2
index 86cdd70c47f..497128c96c5 100644
@@ -342,14 +342,14 @@ static bool translator_ld(CPUArchState *env,
 DisasContextBase *db,
     case 2:
         if (QEMU_IS_ALIGNED(pc, 2)) {
             uint16_t t = qatomic_read((uint16_t *)host);
-            stw_he_p(dest, t);
+            stw_unaligned_p(dest, t);
             return true;
         }
         break;
     case 4:
         if (QEMU_IS_ALIGNED(pc, 4)) {
             uint32_t t = qatomic_read((uint32_t *)host);
-            stl_he_p(dest, t);
+            stl_unaligned_p(dest, t);
             return true;
         }
         break;
accel/tcg/translator.c +1 -1
@@ -357,7 +357,7 @@ static bool translator_ld(CPUArchState *env,
 DisasContextBase *db,
     case 8:
         if (QEMU_IS_ALIGNED(pc, 8)) {
             uint64_t t = qatomic_read__nocheck((uint64_t *)host);
-            stq_he_p(dest, t);
+            stq_unaligned_p(dest, t);
             return true;
         }
         break;
hw/display/ati_2d.c +1 -1
index 24a3c3e942f..333d78d7455 100644
@@ -222,7 +222,7 @@ void ati_2d_blt(ATIVGAState *s)
             for (y = 0; y < s->regs.dst_height; y++) {
                 i = dst_x * bypp + (dst_y + y) * dst_pitch;
                 for (x = 0; x < s->regs.dst_width; x++, i += bypp) {
-                    stn_he_p(&dst_bits[i], bypp, filler);
+                    stn_unaligned_p(&dst_bits[i], bypp, filler);
                 }
             }
         }
hw/display/sm501.c +1 -1
index 51efe2ad41f..776fedebb13 100644
@@ -768,7 +768,7 @@ static void sm501_2d_operation(SM501State *s)
             for (y = 0; y < height; y++) {
                 i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                 for (x = 0; x < width; x++, i += bypp) {
-                    stn_he_p(&d[i], bypp, ~ldn_he_p(&d[i], bypp));
+                    stn_unaligned_p(&d[i], bypp, ~ldn_unaligned_p(&d[i],
 bypp));
                 }
             }
         } else if (!rop_mode && rop == 0x99) {
hw/display/sm501.c +3 -2
@@ -781,8 +781,9 @@ static void sm501_2d_operation(SM501State *s)
                 i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                 j = (src_x + (src_y + y) * src_pitch) * bypp;
                 for (x = 0; x < width; x++, i += bypp, j += bypp) {
-                    stn_he_p(&d[i], bypp,
-                             ~(ldn_he_p(&sp[j], bypp) ^ ldn_he_p(&d[i],
 bypp)));
+                    stn_unaligned_p(&d[i], bypp,
+                                    ~(ldn_unaligned_p(&sp[j], bypp)
+                                      ^ ldn_unaligned_p(&d[i], bypp)));
                 }
             }
         } else if (!rop_mode && rop == 0xee) {
hw/display/sm501.c +3 -2
@@ -795,8 +796,9 @@ static void sm501_2d_operation(SM501State *s)
                 i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                 j = (src_x + (src_y + y) * src_pitch) * bypp;
                 for (x = 0; x < width; x++, i += bypp, j += bypp) {
-                    stn_he_p(&d[i], bypp,
-                             ldn_he_p(&sp[j], bypp) | ldn_he_p(&d[i],
 bypp));
+                    stn_unaligned_p(&d[i], bypp,
+                                    ldn_unaligned_p(&sp[j], bypp)
+                                    | ldn_unaligned_p(&d[i], bypp));
                 }
             }
         } else {
hw/display/sm501.c +3 -2
@@ -818,8 +820,9 @@ static void sm501_2d_operation(SM501State *s)
             if (width == 1 && height == 1) {
                 unsigned int si = (src_x + src_y * src_pitch) * bypp;
                 unsigned int di = (dst_x + dst_y * dst_pitch) * bypp;
-                stn_he_p(&s->local_mem[dst_base + di], bypp,
-                         ldn_he_p(&s->local_mem[src_base + si], bypp));
+                stn_unaligned_p(&s->local_mem[dst_base + di], bypp,
+                                ldn_unaligned_p(&s->local_mem[src_base +
 si],
+                                                bypp));
                 break;
             }
             /* If reverse blit do simple check for overlaps */
hw/display/sm501.c +1 -1
@@ -917,7 +920,7 @@ static void sm501_2d_operation(SM501State *s)
                 for (y = 0; y < height; y++) {
                     i = (dst_x + (dst_y + y) * dst_pitch) * bypp;
                     for (x = 0; x < width; x++, i += bypp) {
-                        stn_he_p(&d[i], bypp, color);
+                        stn_unaligned_p(&d[i], bypp, color);
                     }
                 }
             }
hw/remote/vfio-user-obj.c +1 -1
index 9c9ac8b0d92..44adff10eb9 100644
@@ -392,7 +392,7 @@ static int vfu_object_mr_rw(MemoryRegion *mr, uint8_t
 *buf, hwaddr offset,
         access_size = memory_access_size(mr, size, offset);
 
         if (is_write) {
-            val = ldn_he_p(ptr, access_size);
+            val = ldn_unaligned_p(ptr, access_size);
 
             result = memory_region_dispatch_write(mr, offset, val,

size_memop(access_size),

hw/remote/vfio-user-obj.c +1 -1
@@ -402,7 +402,7 @@ static int vfu_object_mr_rw(MemoryRegion *mr, uint8_t
 *buf, hwaddr offset,
                                                  size_memop(access_size),
                                                  MEMTXATTRS_UNSPECIFIED);
 
-            stn_he_p(ptr, access_size, val);
+            stn_unaligned_p(ptr, access_size, val);
         }
 
         if (release_lock) {
hw/vmapple/virtio-blk.c +1 -1
index f5e8e92df75..805bcbc9467 100644
@@ -79,7 +79,7 @@ static void vmapple_virtio_blk_get_config(VirtIODevice
 *vdev, uint8_t *config)
     g_assert(dev->parent_obj.config_size >= endof(struct
 virtio_blk_config, zoned));
 
     /* Apple abuses the field for max_secure_erase_sectors as type id */
-    stl_he_p(&blkcfg->max_secure_erase_sectors, dev->apple_type);
+    stl_unaligned_p(&blkcfg->max_secure_erase_sectors, dev->apple_type);
 }

static void vmapple_virtio_blk_class_init(ObjectClass *klass, const void
*data)

net/checksum.c +1 -1
index 18be31c26e5..56314f5e76d 100644
@@ -110,7 +110,7 @@ void net_checksum_calculate(void *data, int length,
 int csum_flag)
 
     /* Calculate IP checksum */
     if (csum_flag & CSUM_IP) {
-        stw_he_p(&ip->ip_sum, 0);
+        stw_unaligned_p(&ip->ip_sum, 0);
         csum = net_raw_checksum((uint8_t *)ip, IP_HDR_GET_LEN(ip));
         stw_be_p(&ip->ip_sum, csum);
     }
net/checksum.c +1 -1
@@ -142,7 +142,7 @@ void net_checksum_calculate(void *data, int length,
 int csum_flag)
         }
 
         /* Set csum to 0 */
-        stw_he_p(&tcp->th_sum, 0);
+        stw_unaligned_p(&tcp->th_sum, 0);
 
         csum = net_checksum_tcpudp(ip_len, ip->ip_p,
                                    (uint8_t *)&ip->ip_src,
net/checksum.c +1 -1
@@ -166,7 +166,7 @@ void net_checksum_calculate(void *data, int length,
 int csum_flag)
         }
 
         /* Set csum to 0 */
-        stw_he_p(&udp->uh_sum, 0);
+        stw_unaligned_p(&udp->uh_sum, 0);
 
         csum = net_checksum_tcpudp(ip_len, ip->ip_p,
                                    (uint8_t *)&ip->ip_src,
system/memory.c +1 -1
index 8b84661ae36..653d46e8e79 100644
@@ -1365,7 +1365,7 @@ static uint64_t memory_region_ram_device_read(void
 *opaque,
                                               hwaddr addr, unsigned size)
 {
     MemoryRegion *mr = opaque;
-    uint64_t data = ldn_he_p(mr->ram_block->host + addr, size);
+    uint64_t data = ldn_unaligned_p(mr->ram_block->host + addr, size);
 
     trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data,

size);

system/memory.c +1 -1
@@ -1379,7 +1379,7 @@ static void memory_region_ram_device_write(void
 *opaque, hwaddr addr,
 
     trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data,
 size);
 
-    stn_he_p(mr->ram_block->host + addr, size, data);
+    stn_unaligned_p(mr->ram_block->host + addr, size, data);
 }

static const MemoryRegionOps ram_device_mem_ops = {

system/physmem.c +0 -0
index fb69cdb57d9..d62af6f2480 100644
@@ -3284,7 +3284,7 @@ static MemTxResult
 flatview_write_continue_step(MemTxAttrs attrs,
 
         /*
          * Assure Coverity (and ourselves) that we are not going to

OVERRUN

system/physmem.c +2 -2
-         * the buffer by following ldn_he_p().
+         * the buffer by following ldn_unaligned_p().
          */
 #ifdef QEMU_STATIC_ANALYSIS
         assert((*l == 1 && len >= 1) ||
@@ -3292,7 +3292,7 @@ static MemTxResult
 flatview_write_continue_step(MemTxAttrs attrs,
                (*l == 4 && len >= 4) ||
                (*l == 8 && len >= 8));
 #endif
-        val = ldn_he_p(buf, *l);
+        val = ldn_unaligned_p(buf, *l);
         result = memory_region_dispatch_write(mr, mr_addr, val,
                                               size_memop(*l), attrs);
         if (release_lock) {
system/physmem.c +0 -0
@@ -3379,7 +3379,7 @@ static MemTxResult
 flatview_read_continue_step(MemTxAttrs attrs, uint8_t *buf,
 
         /*
          * Assure Coverity (and ourselves) that we are not going to

OVERRUN

system/physmem.c +2 -2
-         * the buffer by following stn_he_p().
+         * the buffer by following stn_unaligned_p().
          */
 #ifdef QEMU_STATIC_ANALYSIS
         assert((*l == 1 && len >= 1) ||
@@ -3387,7 +3387,7 @@ static MemTxResult
 flatview_read_continue_step(MemTxAttrs attrs, uint8_t *buf,
                (*l == 4 && len >= 4) ||
                (*l == 8 && len >= 8));
 #endif
-        stn_he_p(buf, *l, val);
+        stn_unaligned_p(buf, *l, val);
 
         if (release_lock) {
             bql_unlock();
ui/vnc-enc-tight.c +1 -1
index b645aebccef..62d825dc05f 100644
@@ -912,7 +912,7 @@ static void tight_pack24(VncState *vs, uint8_t *buf,
 size_t count, size_t *ret)
     }
 
     while (count--) {
-        pix = ldl_he_p(buf8);
+        pix = ldl_unaligned_p(buf8);
         *buf++ = (char)(pix >> rshift);
         *buf++ = (char)(pix >> gshift);
         *buf++ = (char)(pix >> bshift);
util/bufferiszero.c +2 -2
index 9548dd3ad1b..d7775dc891d 100644
@@ -38,10 +38,10 @@ static bool buffer_is_zero_int_lt256(const void *buf,
 size_t len)
      * the beginning and end of the buffer.
      */
     if (unlikely(len <= 8)) {
-        return (ldl_he_p(buf) | ldl_he_p(buf + len - 4)) == 0;
+        return (ldl_unaligned_p(buf) | ldl_unaligned_p(buf + len - 4)) ==
 0;
     }
 
-    t = ldq_he_p(buf) | ldq_he_p(buf + len - 8);
+    t = ldq_unaligned_p(buf) | ldq_unaligned_p(buf + len - 8);
     p = QEMU_ALIGN_PTR_DOWN(buf + 8, 8);
     e = QEMU_ALIGN_PTR_DOWN(buf + len - 1, 8);
util/bufferiszero.c +1 -1
@@ -58,7 +58,7 @@ static bool buffer_is_zero_int_ge256(const void *buf,
 size_t len)
      * Use unaligned memory access functions to handle
      * the beginning and end of the buffer.
      */
-    uint64_t t = ldq_he_p(buf) | ldq_he_p(buf + len - 8);
+    uint64_t t = ldq_unaligned_p(buf) | ldq_unaligned_p(buf + len - 8);
     const uint64_t *p = QEMU_ALIGN_PTR_DOWN(buf + 8, 8);
     const uint64_t *e = QEMU_ALIGN_PTR_DOWN(buf + len - 1, 8);
accel/tcg/ldst_atomicity.c.inc +1 -1
index c735add2615..eeb18c716c0 100644
@@ -415,7 +415,7 @@ static uint16_t load_atom_2(CPUState *cpu, uintptr_t
 ra,
     atmax = required_atomicity(cpu, pi, memop);
     switch (atmax) {
     case MO_8:
-        return lduw_he_p(pv);
+        return lduw_unaligned_p(pv);
     case MO_16:
         /* The only case remaining is MO_ATOM_WITHIN16. */
         if (!HAVE_al8_fast && (pi & 3) == 1) {
accel/tcg/ldst_atomicity.c.inc +1 -1
@@ -512,7 +512,7 @@ static uint64_t load_atom_8(CPUState *cpu, uintptr_t
 ra,
     }
     switch (atmax) {
     case MO_8:
-        return ldq_he_p(pv);
+        return ldq_unaligned_p(pv);
     case MO_16:
         return load_atom_8_by_2(pv);
     case MO_32:
accel/tcg/ldst_atomicity.c.inc +3 -4
@@ -875,7 +875,7 @@ static void store_atom_2(CPUState *cpu, uintptr_t ra,
 
     atmax = required_atomicity(cpu, pi, memop);
     if (atmax == MO_8) {
-        stw_he_p(pv, val);
+        stw_unaligned_p(pv, val);
         return;
     }
 
@@ -928,7 +928,7 @@ static void store_atom_4(CPUState *cpu, uintptr_t ra,
     atmax = required_atomicity(cpu, pi, memop);
     switch (atmax) {
     case MO_8:
-        stl_he_p(pv, val);
+        stl_unaligned_p(pv, val);
         return;
     case MO_16:
         store_atom_4_by_2(pv, val);
@@ -996,7 +996,7 @@ static void store_atom_8(CPUState *cpu, uintptr_t ra,
     atmax = required_atomicity(cpu, pi, memop);
     switch (atmax) {
     case MO_8:
-        stq_he_p(pv, val);
+        stq_unaligned_p(pv, val);
         return;
     case MO_16:
         store_atom_8_by_2(pv, val);
--

2.52.0

Reviewed-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] system/memory: Rename unaligned load/store API

In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Rename the API methods using the explicit 'unaligned' description instead of 'he' which stands for 'host endianness'.

Quoted 15 lines
---
  include/qemu/bswap.h                 | 32 ++++++++++++++--------------
  include/qemu/memory_ldst_unaligned.h | 16 +++++++-------
  accel/tcg/translator.c               |  6 +++---
  hw/display/ati_2d.c                  |  2 +-
  hw/display/sm501.c                   | 19 ++++++++++-------
  hw/remote/vfio-user-obj.c            |  4 ++--
  hw/vmapple/virtio-blk.c              |  2 +-
  net/checksum.c                       |  6 +++---
  system/memory.c                      |  4 ++--
  system/physmem.c                     |  8 +++----
  ui/vnc-enc-tight.c                   |  2 +-
  util/bufferiszero.c                  |  6 +++---
  accel/tcg/ldst_atomicity.c.inc       | 10 ++++-----
  13 files changed, 60 insertions(+), 57 deletions(-)

r~