aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / tools / ck_spinlock_asm_check.sh
| 1 | #!/usr/bin/env bash |
| 2 | # ck_spinlock_asm_check.sh A64.dis X86.dis -- reads `objdump -dr` of native/kernel/tests/spinlock_tu.c (function |
| 3 | # ck_spin_asm_locked: ck_spin_lock_irqsave + ck_spin_unlock_irqrestore on struct ck_spinlock ck_spin_asm_lock) built |
| 4 | # freestanding for aarch64 and x86-64 (make spinlock-asm-check, DRA-556). Checks the order core/spinlock.h promises |
| 5 | # and that the atomics touch the right fields (ticket at offset 0, serving at offset 4): |
| 6 | # aarch64 mrs daif < msr daifset #3 < ticket atomic (ldxr/ldaxr/ldadd/cas on [xT]) ; stlr to [xS] < msr daif, |
| 7 | # where xS = xT + 4 (an "add xS, xT, #0x4"). |
| 8 | # x86-64 pushf < cli < lock xadd whose relocation is ck_spin_asm_lock-0x4 (PC32 addend for offset 0) ; |
| 9 | # the last mov to memory before sti has relocation ck_spin_asm_lock (offset 4), then sti. |
| 10 | # Only this one translation unit is checked; the inlined copies elsewhere rely on the same asm volatile "memory" |
| 11 | # clobbers. Last line: CK_SPINLOCK_ASM: PASS|FAIL. |
| 12 | set -uo pipefail |
| 13 | if [[ "${1:-}" == "--selftest" ]]; then |
| 14 | # Negative controls: each mutated dump must FAIL. swap A B: exchange the first line matching A and the last line |
| 15 | # matching B (order mutants); drop A: delete lines matching A. |
| 16 | shift; A=$1; X=$2; t=$(mktemp -d); bad=0 |
| 17 | swap() { awk -v a="$2" -v b="$3" '{l[NR]=$0} $0~a&&!i{i=NR} $0~b{j=NR} END{if(i&&j){t=l[i];l[i]=l[j];l[j]=t} for(k=1;k<=NR;k++)print l[k]}' "$1"; } |
| 18 | neg() { if bash "$0" "$2" "$3" > "$t/out" 2>&1; then cat "$t/out"; echo "NEG FAIL: $1 not caught"; bad=1; |
| 19 | else grep -qF "CK_SPINLOCK_ASM: FAIL" "$t/out" && echo "NEG PASS: $1 caught" || { echo "NEG FAIL: $1 (no verdict)"; bad=1; }; fi; } |
| 20 | grep -v daifset "$A" > "$t/a1"; neg "a64 mask removed" "$t/a1" "$X" |
| 21 | swap "$A" 'daifset' '[[:space:]]ldxr[[:space:]]' > "$t/a2"; neg "a64 ticket atomic before mask" "$t/a2" "$X" |
| 22 | swap "$A" '[[:space:]]stlr[[:space:]]' 'msr[[:space:]]+daif, x' > "$t/a3"; neg "a64 restore before release" "$t/a3" "$X" |
| 23 | grep -vE '[[:space:]]cli' "$X" > "$t/x1"; neg "x86 cli removed" "$A" "$t/x1" |
| 24 | swap "$X" '[[:space:]]cli' 'lock xadd' > "$t/x2"; neg "x86 xadd before cli" "$A" "$t/x2" |
| 25 | sed 's/ck_spin_asm_lock$/ck_spin_asm_count/' "$X" > "$t/x3"; neg "x86 release store to another symbol" "$A" "$t/x3" |
| 26 | rm -rf "$t"; echo "CK_SPINLOCK_ASM_SELFTEST: $([[ $bad == 0 ]] && echo PASS || echo FAIL)"; exit $bad |
| 27 | fi |
| 28 | fn() { awk '/<ck_spin_asm_locked>:/{on=1;next} on&&/^$/{exit} on{print}' "$1"; } |
| 29 | line() { grep -nE "$2" <<<"$1" | head -n1 | cut -d: -f1 || true; } |
| 30 | lastline() { grep -nE "$2" <<<"$1" | tail -n1 | cut -d: -f1 || true; } |
| 31 | fail=0 |
| 32 | check() { if [[ -n "$2" && -n "$3" && "$2" -lt "$3" ]]; then echo "PASS $1 (line $2 < line $3)"; else echo "FAIL $1 (lines '$2' '$3')"; fail=1; fi; } |
| 33 | truth() { if [[ "$2" == 1 ]]; then echo "PASS $1"; else echo "FAIL $1"; fail=1; fi; } |
| 34 | |
| 35 | a=$(fn "$1") |
| 36 | mrs=$(line "$a" 'mrs[[:space:]]+x[0-9]+, daif') |
| 37 | set3=$(line "$a" 'msr[[:space:]]+daifset, #0x3') |
| 38 | atom=$(line "$a" '[[:space:]](ldxr|ldaxr|ldadd[a-z]*|cas[a-z]*)[[:space:]]') |
| 39 | stlr=$(lastline "$a" '[[:space:]]stlr[[:space:]]') |
| 40 | rest=$(lastline "$a" 'msr[[:space:]]+daif, x[0-9]+') |
| 41 | check "a64: DAIF saved before the mask" "$mrs" "$set3" |
| 42 | check "a64: mask (daifset #3) before the ticket atomic" "$set3" "$atom" |
| 43 | check "a64: releasing stlr before msr daif restore" "$stlr" "$rest" |
| 44 | treg=$(sed -n "${atom:-0}p" <<<"$a" | grep -oE '\[x[0-9]+\]' | tr -d '[]' || true) |
| 45 | sreg=$(sed -n "${stlr:-0}p" <<<"$a" | grep -oE '\[x[0-9]+\]' | tr -d '[]' || true) |
| 46 | ok=0; [[ -n "$treg" && -n "$sreg" ]] && grep -qE "add[[:space:]]+$sreg, $treg, #0x4" <<<"$a" && ok=1 |
| 47 | truth "a64: stlr writes serving (ticket base $treg + 4 = $sreg)" "$ok" |
| 48 | |
| 49 | x=$(fn "$2") |
| 50 | pushf=$(line "$x" 'pushf') |
| 51 | cli=$(line "$x" '[[:space:]]cli') |
| 52 | xadd=$(line "$x" 'lock xadd') |
| 53 | sti=$(lastline "$x" '[[:space:]]sti') |
| 54 | rel=$(awk -v s="${sti:-0}" 'NR<s && /mov[l]?[[:space:]]+%[a-z0-9]+,.*\(%rip\)/{n=NR} END{print n}' <<<"$x") |
| 55 | check "x86: RFLAGS read (pushf) before cli" "$pushf" "$cli" |
| 56 | check "x86: cli before the ticket xadd" "$cli" "$xadd" |
| 57 | check "x86: releasing store before sti" "$rel" "$sti" |
| 58 | reloc_after() { awk -v n="${2:-0}" 'NR>n && /R_X86_64_PC32/{print $NF; exit}' <<<"$1"; } |
| 59 | ok=0; [[ "$(reloc_after "$x" "$xadd")" == "ck_spin_asm_lock-0x4" ]] && ok=1 |
| 60 | truth "x86: xadd targets ticket (reloc ck_spin_asm_lock-0x4)" "$ok" |
| 61 | ok=0; [[ "$(reloc_after "$x" "$rel")" == "ck_spin_asm_lock" ]] && ok=1 |
| 62 | truth "x86: releasing store targets serving (reloc ck_spin_asm_lock)" "$ok" |
| 63 | echo "CK_SPINLOCK_ASM: $([[ $fail == 0 ]] && echo PASS || echo FAIL)" |
| 64 | exit $fail |