Skip to contentSkip to conversation

Unaligned access for user only

PATCH Superseded Richard Henderson 5 participants 13 messages Jan 4, 2022 – Jan 7, 2022 RISC-V

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Unaligned access for user only

Version 3 was way back in August:

https://lore.kernel.org/qemu-devel/[email protected]/

Quite a few of the patches in there have been merged, but not all.

Based-on: <[email protected]>

There are follow-on patch sets for arm, mips, and sparc, which I will be refreshing soon. Xuerui, I believe that tcg/loongarch should be as simple as one of these five.

r~

Richard Henderson (7):
tcg/i386: Support raising sigbus for user-only tcg/aarch64: Support raising sigbus for user-only tcg/ppc: Support raising sigbus for user-only tcg/riscv: Support raising sigbus for user-only tcg/s390x: Support raising sigbus for user-only tcg/tci: Support raising sigbus for user-only tests/tcg/multiarch: Add sigbus.c

 tcg/aarch64/tcg-target.h     |   2 -
 tcg/i386/tcg-target.h        |   2 -
 tcg/ppc/tcg-target.h         |   2 -
 tcg/riscv/tcg-target.h       |   2 -
 tcg/s390x/tcg-target.h       |   2 -
 tcg/tci.c                    |  20 +++++--
 tests/tcg/multiarch/sigbus.c |  68 +++++++++++++++++++++++
 tcg/aarch64/tcg-target.c.inc |  91 +++++++++++++++++++++++++------
 tcg/i386/tcg-target.c.inc    | 103 +++++++++++++++++++++++++++++++++--
 tcg/ppc/tcg-target.c.inc     |  98 ++++++++++++++++++++++++++++++---
 tcg/riscv/tcg-target.c.inc   |  63 ++++++++++++++++++++-
 tcg/s390x/tcg-target.c.inc   |  59 +++++++++++++++++++-
 12 files changed, 462 insertions(+), 50 deletions(-)
 create mode 100644 tests/tcg/multiarch/sigbus.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v4 1/7 tcg/i386: Support raising sigbus for user-only

---
 tcg/i386/tcg-target.h     |   2 -
 tcg/i386/tcg-target.c.inc | 103 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 98 insertions(+), 7 deletions(-)
tcg/i386/tcg-target.h +0 -2
index b00a6da293..3b2c9437a0 100644
@@ -232,9 +232,7 @@ static inline void tb_target_set_jmp_target(uintptr_t tc_ptr, uintptr_t jmp_rx,
 
 #define TCG_TARGET_HAS_MEMORY_BSWAP  have_movbe
 
-#ifdef CONFIG_SOFTMMU
 #define TCG_TARGET_NEED_LDST_LABELS
-#endif
 #define TCG_TARGET_NEED_POOL_LABELS
 
 #endif
tcg/i386/tcg-target.c.inc +98 -5

163 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

PATCH v4 2/7 tcg/aarch64: Support raising sigbus for user-only

In reply to Richard Henderson
---
 tcg/aarch64/tcg-target.h     |  2 -
 tcg/aarch64/tcg-target.c.inc | 91 +++++++++++++++++++++++++++++-------
 2 files changed, 74 insertions(+), 19 deletions(-)
tcg/aarch64/tcg-target.h +0 -2
index 7a93ac8023..876af589ce 100644
@@ -151,9 +151,7 @@ typedef enum {
 
 void tb_target_set_jmp_target(uintptr_t, uintptr_t, uintptr_t, uintptr_t);
 
-#ifdef CONFIG_SOFTMMU
 #define TCG_TARGET_NEED_LDST_LABELS
-#endif
 #define TCG_TARGET_NEED_POOL_LABELS
 
 #endif /* AARCH64_TCG_TARGET_H */
tcg/aarch64/tcg-target.c.inc +74 -17

164 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw

PATCH v4 3/7 tcg/ppc: Support raising sigbus for user-only

In reply to Richard Henderson
---
 tcg/ppc/tcg-target.h     |  2 -
 tcg/ppc/tcg-target.c.inc | 98 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 90 insertions(+), 10 deletions(-)
tcg/ppc/tcg-target.h +0 -2
index 0943192cde..c775c97b61 100644
@@ -182,9 +182,7 @@ void tb_target_set_jmp_target(uintptr_t, uintptr_t, uintptr_t, uintptr_t);
 #define TCG_TARGET_DEFAULT_MO (0)
 #define TCG_TARGET_HAS_MEMORY_BSWAP     1
 
-#ifdef CONFIG_SOFTMMU
 #define TCG_TARGET_NEED_LDST_LABELS
-#endif
 #define TCG_TARGET_NEED_POOL_LABELS
 
 #endif
tcg/ppc/tcg-target.c.inc +90 -8

171 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw

PATCH v4 4/7 tcg/riscv: Support raising sigbus for user-only

In reply to Richard Henderson
---
 tcg/riscv/tcg-target.h     |  2 --
 tcg/riscv/tcg-target.c.inc | 63 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 61 insertions(+), 4 deletions(-)
tcg/riscv/tcg-target.h +0 -2
index ef78b99e98..11c9b3e4f4 100644
@@ -165,9 +165,7 @@ void tb_target_set_jmp_target(uintptr_t, uintptr_t, uintptr_t, uintptr_t);
 
 #define TCG_TARGET_DEFAULT_MO (0)
 
-#ifdef CONFIG_SOFTMMU
 #define TCG_TARGET_NEED_LDST_LABELS
-#endif
 #define TCG_TARGET_NEED_POOL_LABELS
 
 #define TCG_TARGET_HAS_MEMORY_BSWAP 0
tcg/riscv/tcg-target.c.inc +61 -2

105 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

PATCH v4 5/7 tcg/s390x: Support raising sigbus for user-only

In reply to Richard Henderson
---
 tcg/s390x/tcg-target.h     |  2 --
 tcg/s390x/tcg-target.c.inc | 59 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 57 insertions(+), 4 deletions(-)
tcg/s390x/tcg-target.h +0 -2
index 527ada0f63..69217d995b 100644
@@ -178,9 +178,7 @@ static inline void tb_target_set_jmp_target(uintptr_t tc_ptr, uintptr_t jmp_rx,
     /* no need to flush icache explicitly */
 }
 
-#ifdef CONFIG_SOFTMMU
 #define TCG_TARGET_NEED_LDST_LABELS
-#endif
 #define TCG_TARGET_NEED_POOL_LABELS
 
 #endif
tcg/s390x/tcg-target.c.inc +57 -2

97 lines not on this page · raw message

Signature · 1 line
2.25.1
#7 lore raw

PATCH v4 6/7 tcg/tci: Support raising sigbus for user-only

In reply to Richard Henderson
---
 tcg/tci.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
tcg/tci.c +14 -6
index e76087ccac..92a7c81674 100644
@@ -292,11 +292,11 @@ static bool tci_compare64(uint64_t u0, uint64_t u1, TCGCond condition)
 static uint64_t tci_qemu_ld(CPUArchState *env, target_ulong taddr,
                             MemOpIdx oi, const void *tb_ptr)
 {
-    MemOp mop = get_memop(oi) & (MO_BSWAP | MO_SSIZE);
+    MemOp mop = get_memop(oi);
     uintptr_t ra = (uintptr_t)tb_ptr;
 
 #ifdef CONFIG_SOFTMMU
-    switch (mop) {
+    switch (mop & (MO_BSWAP | MO_SSIZE)) {
     case MO_UB:
         return helper_ret_ldub_mmu(env, taddr, oi, ra);
     case MO_SB:
@@ -326,10 +326,14 @@ static uint64_t tci_qemu_ld(CPUArchState *env, target_ulong taddr,
     }
 #else
     void *haddr = g2h(env_cpu(env), taddr);
+    unsigned a_mask = (1u << get_alignment_bits(mop)) - 1;
     uint64_t ret;
 
     set_helper_retaddr(ra);
-    switch (mop) {
+    if (taddr & a_mask) {
+        helper_unaligned_ld(env, taddr);
+    }
+    switch (mop & (MO_BSWAP | MO_SSIZE)) {
     case MO_UB:
         ret = ldub_p(haddr);
         break;
@@ -377,11 +381,11 @@ static uint64_t tci_qemu_ld(CPUArchState *env, target_ulong taddr,
 static void tci_qemu_st(CPUArchState *env, target_ulong taddr, uint64_t val,
                         MemOpIdx oi, const void *tb_ptr)
 {
-    MemOp mop = get_memop(oi) & (MO_BSWAP | MO_SSIZE);
+    MemOp mop = get_memop(oi);
     uintptr_t ra = (uintptr_t)tb_ptr;
 
 #ifdef CONFIG_SOFTMMU
-    switch (mop) {
+    switch (mop & (MO_BSWAP | MO_SIZE)) {
     case MO_UB:
         helper_ret_stb_mmu(env, taddr, val, oi, ra);
         break;
@@ -408,9 +412,13 @@ static void tci_qemu_st(CPUArchState *env, target_ulong taddr, uint64_t val,
     }
 #else
     void *haddr = g2h(env_cpu(env), taddr);
+    unsigned a_mask = (1u << get_alignment_bits(mop)) - 1;
 
     set_helper_retaddr(ra);
-    switch (mop) {
+    if (taddr & a_mask) {
+        helper_unaligned_st(env, taddr);
+    }
+    switch (mop & (MO_BSWAP | MO_SIZE)) {
     case MO_UB:
         stb_p(haddr, val);
         break;
Signature · 1 line
2.25.1
#9 lore raw

PATCH v4 7/7 tests/tcg/multiarch: Add sigbus.c

In reply to Richard Henderson

A mostly generic test for unaligned access raising SIGBUS.

---
 tests/tcg/multiarch/sigbus.c | 68 ++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 tests/tcg/multiarch/sigbus.c
tests/tcg/multiarch/sigbus.c new file +68 -0
new file mode 100644
index 0000000000..8134c5fd56
@@ -0,0 +1,68 @@
+#define _GNU_SOURCE 1
+
+#include <assert.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <endian.h>
+
+
+unsigned long long x = 0x8877665544332211ull;
+void * volatile p = (void *)&x + 1;
+
+void sigbus(int sig, siginfo_t *info, void *uc)
+{
+    assert(sig == SIGBUS);
+    assert(info->si_signo == SIGBUS);
+#ifdef BUS_ADRALN
+    assert(info->si_code == BUS_ADRALN);
+#endif
+    assert(info->si_addr == p);
+    exit(EXIT_SUCCESS);
+}
+
+int main()
+{
+    struct sigaction sa = {
+        .sa_sigaction = sigbus,
+        .sa_flags = SA_SIGINFO
+    };
+    int allow_fail = 0;
+    int tmp;
+
+    tmp = sigaction(SIGBUS, &sa, NULL);
+    assert(tmp == 0);
+
+    /*
+     * Select an operation that's likely to enforce alignment.
+     * On many guests that support unaligned accesses by default,
+     * this is often an atomic operation.
+     */
+#if defined(__aarch64__)
+    asm volatile("ldxr %w0,[%1]" : "=r"(tmp) : "r"(p) : "memory");
+#elif defined(__alpha__)
+    asm volatile("ldl_l %0,0(%1)" : "=r"(tmp) : "r"(p) : "memory");
+#elif defined(__arm__)
+    asm volatile("ldrex %0,[%1]" : "=r"(tmp) : "r"(p) : "memory");
+#elif defined(__powerpc__)
+    asm volatile("lwarx %0,0,%1" : "=r"(tmp) : "r"(p) : "memory");
+#elif defined(__riscv_atomic)
+    asm volatile("lr.w %0,(%1)" : "=r"(tmp) : "r"(p) : "memory");
+#else
+    /* No insn known to fault unaligned -- try for a straight load. */
+    allow_fail = 1;
+    tmp = *(volatile int *)p;
+#endif
+
+    assert(allow_fail);
+
+    /*
+     * We didn't see a signal.
+     * We might as well validate the unaligned load worked.
+     */
+    if (BYTE_ORDER == LITTLE_ENDIAN) {
+        assert(tmp == 0x55443322);
+    } else {
+        assert(tmp == 0x77665544);
+    }
+    return EXIT_SUCCESS;
+}
Signature · 1 line
2.25.1
Reviewed-by on v4 7/7
#10 lore raw

Re: [PATCH v4 7/7] tests/tcg/multiarch: Add sigbus.c

Quoted 8 lines · Richard Henderson

A mostly generic test for unaligned access raising SIGBUS.

---
  tests/tcg/multiarch/sigbus.c | 68 ++++++++++++++++++++++++++++++++++++
  1 file changed, 68 insertions(+)

create mode 100644 tests/tcg/multiarch/sigbus.c

#12 lore raw
In reply to Richard Henderson

Hi Richard,

Quoted 11 lines · Richard Henderson

Version 3 was way back in August:

https://lore.kernel.org/qemu-devel/[email protected]/

Quite a few of the patches in there have been merged, but not all.

Based-on: <[email protected]>

There are follow-on patch sets for arm, mips, and sparc, which I will be refreshing soon. Xuerui, I believe that tcg/loongarch should be as simple as one of these five.

Thanks for the heads-up; I'll take care of implementing the loongarch64 part in this week (or maybe next week in case of $DAY_JOB).

Quoted 29 lines

r~

Richard Henderson (7):
tcg/i386: Support raising sigbus for user-only tcg/aarch64: Support raising sigbus for user-only tcg/ppc: Support raising sigbus for user-only tcg/riscv: Support raising sigbus for user-only tcg/s390x: Support raising sigbus for user-only tcg/tci: Support raising sigbus for user-only tests/tcg/multiarch: Add sigbus.c

  tcg/aarch64/tcg-target.h     |   2 -
  tcg/i386/tcg-target.h        |   2 -
  tcg/ppc/tcg-target.h         |   2 -
  tcg/riscv/tcg-target.h       |   2 -
  tcg/s390x/tcg-target.h       |   2 -
  tcg/tci.c                    |  20 +++++--
  tests/tcg/multiarch/sigbus.c |  68 +++++++++++++++++++++++
  tcg/aarch64/tcg-target.c.inc |  91 +++++++++++++++++++++++++------
  tcg/i386/tcg-target.c.inc    | 103 +++++++++++++++++++++++++++++++++--
  tcg/ppc/tcg-target.c.inc     |  98 ++++++++++++++++++++++++++++++---
  tcg/riscv/tcg-target.c.inc   |  63 ++++++++++++++++++++-
  tcg/s390x/tcg-target.c.inc   |  59 +++++++++++++++++++-
  12 files changed, 462 insertions(+), 50 deletions(-)

create mode 100644 tests/tcg/multiarch/sigbus.c