diff --git a/tools/linuxfs/lse-emu/README.md b/tools/linuxfs/lse-emu/README.md index e406fee811..90db0de80d 100644 --- a/tools/linuxfs/lse-emu/README.md +++ b/tools/linuxfs/lse-emu/README.md @@ -1,7 +1,9 @@ # LSE-Emu -`lse_emu.c` is an unmodified copy of [SildurFX/LSE-Emu](https://github.com/SildurFX/LSE-Emu) -at commit `8bc9548ac6fd24a8d506b266b17ba8d0e71a9b01` (GPL-3.0-or-later). +`lse_emu.c` is a copy of [SildurFX/LSE-Emu](https://github.com/SildurFX/LSE-Emu) at commit +`8bc9548ac6fd24a8d506b266b17ba8d0e71a9b01` (GPL-3.0-or-later) with one fix: the patched stub +for `LDSMAX`/`LDSMIN`/`LDUMAX`/`LDUMIN` saves and restores NZCV around its `cmp`/`csel`, since +the real instructions leave the flags alone. Carry that fix forward until upstream has it. It is an `LD_PRELOAD` library that emulates the ARMv8.1 LSE atomic instructions (and `LDAPR`) on ARMv8.0 CPUs. A SIGILL handler performs each trapped instruction with an LL/SC loop. With diff --git a/tools/linuxfs/lse-emu/build.sh b/tools/linuxfs/lse-emu/build.sh index 4c4a6d5e02..321c917b13 100644 --- a/tools/linuxfs/lse-emu/build.sh +++ b/tools/linuxfs/lse-emu/build.sh @@ -9,6 +9,6 @@ mkdir -p "$(dirname "$out")" "$CC" -shared -fPIC -O2 -march=armv8-a -mno-outline-atomics -Wall -Wextra -Wno-unused-parameter \ -Wl,-z,now,-z,max-page-size=65536 -o "$out" "$here/lse_emu.c" -ldl "$STRIP" --strip-unneeded "$out" -if "${OBJDUMP:-aarch64-linux-gnu-objdump}" -d "$out" | grep -qE $'\t(ld(add|clr|eor|set|smax|smin|umax|umin)|swp|cas)[a-z]*\t'; then +if "${OBJDUMP:-aarch64-linux-gnu-objdump}" -d "$out" | grep -qE $'\t((ld|st)(add|clr|eor|set|smax|smin|umax|umin)|swp|cas)[a-z]*\t'; then echo "::error::liblse_emu.so itself contains LSE instructions"; exit 1 fi diff --git a/tools/linuxfs/lse-emu/lse_emu.c b/tools/linuxfs/lse-emu/lse_emu.c index 630f4e887e..e891a137ad 100644 --- a/tools/linuxfs/lse-emu/lse_emu.c +++ b/tools/linuxfs/lse-emu/lse_emu.c @@ -246,6 +246,10 @@ static int gen(uint32_t *w, uintptr_t stub, uintptr_t ret, uint32_t i) { W(STP2(t0, t1)); W(STR16(t2)); + if (kind == 4) { // cmp/csel below clobber NZCV, the real instruction does not + W(0xD53B4200u | t2); // mrs t2, nzcv + W(0xF9000C00u | (31u << 5) | t2); // str t2,[sp,#24] + } int loop = n; W(ldx(size, acq, t0, rn)); // t0 = old if (kind == 1) { @@ -283,6 +287,10 @@ static int gen(uint32_t *w, uintptr_t stub, uintptr_t ret, uint32_t i) { } if (kind == 2) { if (rs != 31) W(MOVX(rs, t0)); } else if (rt != 31) W(MOVX(rt, t0)); + if (kind == 4) { + W(0xF9400C00u | (31u << 5) | t2); // ldr t2,[sp,#24] + W(0xD51B4200u | t2); // msr nzcv, t2 + } W(LDR16(t2)); W(LDP2(t0, t1)); { int64_t off = (int64_t)ret - (int64_t)(stub + 4 * (uintptr_t)n); diff --git a/tools/tests/lse_emu_test.c b/tools/tests/lse_emu_test.c index 9db8cf6e7d..402dc1fca1 100644 --- a/tools/tests/lse_emu_test.c +++ b/tools/tests/lse_emu_test.c @@ -74,9 +74,7 @@ static void app_sigill(int sig, siginfo_t *info, void *context) { ((ucontext_t *)context)->uc_mcontext.pc += 4; } -int main(int argc, char **argv) { - int threads_wanted = argc > 1 ? atoi(argv[1]) : 4; - check(threads_wanted >= 1 && threads_wanted <= MAX_THREADS, "thread count"); +static __attribute__((noinline)) void single_forms(void) { uint64_t v = 0xF0; uint64_t old; __asm__ volatile("ldclral %x1, %x0, [%2]" : "=r"(old) : "r"((uint64_t)0x30), "r"(&v) : "memory"); @@ -105,6 +103,25 @@ int main(int argc, char **argv) { uint64_t missed = casal64(&v, 99, 1); check(missed == 7 && v == 7, "casal leaves memory alone on a mismatch"); + uint32_t m = 1; + uint32_t mold; + uint64_t eq; + __asm__ volatile("cmp %w3, %w3\n\tldumax %w2, %w0, [%4]\n\tcset %1, eq" + : "=&r"(mold), "=&r"(eq) : "r"((uint32_t)5), "r"((uint32_t)0), "r"(&m) : "memory", "cc"); + check(mold == 1 && m == 5 && eq == 1, "ldumax leaves the condition flags alone"); + int8_t b = -3; + int8_t bold; + __asm__ volatile("cmp %w3, %w3\n\tldsmaxb %w2, %w0, [%4]\n\tcset %1, eq" + : "=&r"(bold), "=&r"(eq) : "r"((int32_t)-7), "r"((uint32_t)0), "r"(&b) : "memory", "cc"); + check(bold == -3 && b == -3 && eq == 1, "ldsmaxb leaves the condition flags alone"); +} + +int main(int argc, char **argv) { + int threads_wanted = argc > 1 ? atoi(argv[1]) : 4; + check(threads_wanted >= 1 && threads_wanted <= MAX_THREADS, "thread count"); + single_forms(); + single_forms(); + pthread_t threads[MAX_THREADS]; for (int i = 0; i < threads_wanted; i++) pthread_create(&threads[i], NULL, worker, NULL); for (int i = 0; i < threads_wanted; i++) pthread_join(threads[i], NULL);