[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 44/55] tests/tcg/multiarch: Add sigbus.c
From: |
Richard Henderson |
Subject: |
[PATCH v2 44/55] tests/tcg/multiarch: Add sigbus.c |
Date: |
Mon, 2 Aug 2021 18:14:32 -1000 |
A mostly generic test for unaligned access raising SIGBUS.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
tests/tcg/multiarch/sigbus.c | 68 ++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)
create mode 100644 tests/tcg/multiarch/sigbus.c
diff --git a/tests/tcg/multiarch/sigbus.c b/tests/tcg/multiarch/sigbus.c
new file mode 100644
index 0000000000..8134c5fd56
--- /dev/null
+++ b/tests/tcg/multiarch/sigbus.c
@@ -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;
+}
--
2.25.1
- Re: [PATCH v2 37/55] target/mips: Use 8-byte memory ops for msa load/store, (continued)
- [PATCH v2 38/55] target/s390x: Use cpu_*_mmu instead of helper_*_mmu, Richard Henderson, 2021/08/03
- [PATCH v2 32/55] target/s390x: Use MO_128 for 16 byte atomics, Richard Henderson, 2021/08/03
- [PATCH v2 36/55] target/mips: Use cpu_*_data_ra for msa load/store, Richard Henderson, 2021/08/03
- [PATCH v2 40/55] target/arm: Use cpu_*_mmu instead of helper_*_mmu, Richard Henderson, 2021/08/03
- [PATCH v2 41/55] tcg: Move helper_*_mmu decls to tcg/tcg-ldst.h, Richard Henderson, 2021/08/03
- [PATCH v2 42/55] tcg: Add helper_unaligned_mmu for user-only sigbus, Richard Henderson, 2021/08/03
- [PATCH v2 43/55] tcg/i386: Support raising sigbus for user-only, Richard Henderson, 2021/08/03
- [PATCH v2 44/55] tests/tcg/multiarch: Add sigbus.c,
Richard Henderson <=
- [PATCH v2 46/55] linux-user: Disable more prctl subcodes, Richard Henderson, 2021/08/03
- [PATCH v2 47/55] hw/core/cpu: Re-sort the non-pointers to the end of CPUClass, Richard Henderson, 2021/08/03
- [PATCH v2 45/55] linux-user: Split out do_prctl and subroutines, Richard Henderson, 2021/08/03
- [PATCH v2 48/55] linux-user: Add code for PR_GET/SET_UNALIGN, Richard Henderson, 2021/08/03
- [PATCH v2 49/55] hw/core/cpu: Move cpu properties to cpu-sysemu.c, Richard Henderson, 2021/08/03
- [PATCH v2 50/55] hw/core/cpu: Add prctl-unalign-sigbus property for user-only, Richard Henderson, 2021/08/03
- [PATCH v2 51/55] target/alpha: Reorg fp memory operations, Richard Henderson, 2021/08/03
- [PATCH v2 52/55] target/alpha: Reorg integer memory operations, Richard Henderson, 2021/08/03
- [PATCH v2 53/55] target/alpha: Implement prctl_unalign_sigbus, Richard Henderson, 2021/08/03
- [PATCH v2 54/55] target/hppa: Implement prctl_unalign_sigbus, Richard Henderson, 2021/08/03