mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
fix: validate the CASP pair on a mismatch in the LSE emulator
This commit is contained in:
1 parent
ffac895afc
commit
0db9865045
6 files changed
+54
-18
No files matched your search
@@ -1,9 +1,10 @@
|
||||
# LSE-Emu
|
||||
|
||||
`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.
|
||||
`8bc9548ac6fd24a8d506b266b17ba8d0e71a9b01` (GPL-3.0-or-later) with two fixes, listed in
|
||||
`docs/arm-v8.0-steam-client.md` under "Fixes over upstream": NZCV kept across the min/max
|
||||
stubs, and a store-back on `CASP` mismatches so the returned pair is atomic. Carry them forward
|
||||
until upstream has them.
|
||||
|
||||
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
|
||||
|
||||
@@ -90,7 +90,10 @@ static void casp128(void *p, uint64_t *o0, uint64_t *o1,
|
||||
" cbnz %2, 2f\n"
|
||||
" stlxp %w2, %9, %10, [%6]\n"
|
||||
" cbnz %w2, 1b\n"
|
||||
"2: clrex\n"
|
||||
" b 3f\n"
|
||||
"2: stlxp %w2, %0, %1, [%6]\n" // a 128-bit ldxp is only single-copy atomic once a stxp succeeds
|
||||
" cbnz %w2, 1b\n"
|
||||
"3:\n"
|
||||
: "=&r"(a), "=&r"(b), "=&r"(t), "=&r"(s)
|
||||
: "0"(0), "1"(0), "r"(p), "r"(e0), "r"(e1), "r"(d0), "r"(d1)
|
||||
: "memory", "cc");
|
||||
@@ -180,7 +183,7 @@ static uint32_t alu(unsigned op, unsigned d, unsigned n, unsigned m) {
|
||||
return base[op] | (m << 16) | (n << 5) | d; // add, bic, eor, orr
|
||||
}
|
||||
|
||||
// CASP{A}{L} Xs,Xs+1,Xt,Xt+1,[Xn] (64-bit pair): ldxp/ldaxp + compare + stxp/stlxp. Four scratch registers, no flag changes, 15 words.
|
||||
// CASP{A}{L} Xs,Xs+1,Xt,Xt+1,[Xn] (64-bit pair): ldxp/ldaxp + compare + stxp/stlxp. Four scratch registers, no flag changes, 16 words.
|
||||
static int gen_casp(uint32_t *w, uintptr_t stub, uintptr_t ret, unsigned sz,
|
||||
unsigned rs, unsigned rt, unsigned rn, unsigned acq, unsigned rel) {
|
||||
// sz 1: 64-bit pair (LDXP x), sz 0: 32-bit pair (LDXP w, 8-byte aligned)
|
||||
@@ -198,10 +201,11 @@ static int gen_casp(uint32_t *w, uintptr_t stub, uintptr_t ret, unsigned sz,
|
||||
W(EOR | (rs << 16) | (t0 << 5) | t2); // eor t2,t0,rs
|
||||
W(EOR | ((rs + 1) << 16) | (t1 << 5) | t3); // eor t3,t1,rs+1
|
||||
W(ORR | (t3 << 16) | (t2 << 5) | t2); // orr t2,t2,t3
|
||||
W(0xB5000000u | (3u << 5) | t2); // cbnz t2, clrex (+3)
|
||||
W(STXP | (t2 << 16) | (rel << 15) | ((rt + 1) << 10) | (rn << 5) | rt); // stxp/stlxp w(t2),rt,rt+1,[rn]
|
||||
W(0x35000000u | (((unsigned)(loop - n) & 0x7FFFF) << 5) | t2); // cbnz w(t2), loop
|
||||
W(0xD5033F5Fu); // clrex
|
||||
W(0xB4000000u | (3u << 5) | t2); // cbz t2, match (+3)
|
||||
W(STXP | (t2 << 16) | (rel << 15) | (t1 << 10) | (rn << 5) | t0); // mismatch: store the loaded pair back, which validates it
|
||||
W(0x14000002u); // b check (+2)
|
||||
W(STXP | (t2 << 16) | (rel << 15) | ((rt + 1) << 10) | (rn << 5) | rt); // match: stxp/stlxp w(t2),rt,rt+1,[rn]
|
||||
W(0x35000000u | (((unsigned)(loop - n) & 0x7FFFF) << 5) | t2); // check: cbnz w(t2), loop
|
||||
W(MOVX(rs, t0));
|
||||
W(MOVX(rs + 1, t1));
|
||||
W(0xA9410000u | (t3 << 10) | (31u << 5) | t2); // ldp t2,t3,[sp,#16]
|
||||
|
||||
@@ -12,7 +12,7 @@ if [ "${BL_RAISE_NOFILE:-0}" = 1 ] && [ -z "${BL_INSIDE:-}" ]; then
|
||||
fi
|
||||
export HOME=${HOME:-/root}
|
||||
if grep -qs '/liblse_emu\.so$' /etc/ld.so.preload; then
|
||||
export LSE_EMU=1 LSE_EMU_PATCH=1
|
||||
export LSE_EMU=1 LSE_EMU_PATCH=${LSE_EMU_PATCH:-1}
|
||||
[ -n "${BL_DEBUG_DIR:-}" ] && export LSE_EMU_LOG=${LSE_EMU_LOG:-$BL_DEBUG_DIR/lse_emu.log}
|
||||
fi
|
||||
mode=${1:-desktop}
|
||||
|
||||
@@ -99,6 +99,9 @@ static __attribute__((noinline)) void single_forms(void) {
|
||||
register uint64_t e0 __asm__("x4") = 1, e1 __asm__("x5") = 2, d0 __asm__("x6") = 3, d1 __asm__("x7") = 4;
|
||||
__asm__ volatile("caspal x4, x5, x6, x7, [%4]" : "+r"(e0), "+r"(e1) : "r"(d0), "r"(d1), "r"(pair) : "memory");
|
||||
check(e0 == 1 && e1 == 2 && pair[0] == 3 && pair[1] == 4, "caspal");
|
||||
register uint64_t m0 __asm__("x4") = 8, m1 __asm__("x5") = 9, n0 __asm__("x6") = 5, n1 __asm__("x7") = 6;
|
||||
__asm__ volatile("caspal x4, x5, x6, x7, [%4]" : "+r"(m0), "+r"(m1) : "r"(n0), "r"(n1), "r"(pair) : "memory");
|
||||
check(m0 == 3 && m1 == 4 && pair[0] == 3 && pair[1] == 4, "caspal returns the current pair on a mismatch");
|
||||
|
||||
uint64_t missed = casal64(&v, 99, 1);
|
||||
check(missed == 7 && v == 7, "casal leaves memory alone on a mismatch");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
@@ -11,6 +12,8 @@ SYSROOT = Path("/usr/aarch64-linux-gnu")
|
||||
|
||||
def toolchain():
|
||||
if not (shutil.which("aarch64-linux-gnu-gcc") and shutil.which("qemu-aarch64") and SYSROOT.is_dir()):
|
||||
if os.environ.get("CI"):
|
||||
raise AssertionError("CI must install gcc-aarch64-linux-gnu, libc6-dev-arm64-cross and qemu-user")
|
||||
raise unittest.SkipTest("needs aarch64-linux-gnu-gcc, libc6-dev-arm64-cross and qemu-user")
|
||||
|
||||
|
||||
@@ -46,15 +49,19 @@ class LseEmuTest(unittest.TestCase):
|
||||
self.assertIn("lse ok", result.stdout)
|
||||
self.assertRegex(result.stderr, r"traps=[1-9]\d* patched=0 ")
|
||||
|
||||
def test_patching_runs_every_form_on_an_armv8_0_cpu(self):
|
||||
# qemu-user write-protects translated code pages and races when other threads run them while the
|
||||
# stub arena is written, so threaded patching is only meaningful on hardware.
|
||||
def patched(self, threads):
|
||||
result = self.run_on("cortex-a57", f"LD_PRELOAD={self.library}", "LSE_EMU=1", "LSE_EMU_PATCH=1",
|
||||
"LSE_EMU_STATS=1", threads=1)
|
||||
"LSE_EMU_STATS=1", threads=threads)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
self.assertIn("lse ok", result.stdout)
|
||||
self.assertRegex(result.stderr, r"patched=[1-9]\d* failed=0")
|
||||
|
||||
def test_patching_runs_every_form_on_an_armv8_0_cpu(self):
|
||||
self.patched(threads=1)
|
||||
|
||||
def test_patching_stays_atomic_across_threads(self):
|
||||
self.patched(threads=4)
|
||||
|
||||
def test_it_stays_off_without_the_switch(self):
|
||||
result = self.run_on("cortex-a57", f"LD_PRELOAD={self.library}")
|
||||
self.assertEqual(result.returncode, -4, result.stdout + result.stderr)
|
||||
|
||||
Reference in new issue
Block a user