interplane.Get Omega
Read in your language

omega-gpu-driver

aien-dev/omega-gpu-driver

Brought by drakestapleton

branch main Branches Tags History

omega-gpu-driver / tests / window_sim.sh

462 lines · 24613 bytes Raw Blame History
1#!/bin/sh
2# tests/window_sim.sh -- run the real window scripts (window/*.sh, copied unchanged) against a simulated Spark
3# under /tmp: fake sysfs, /etc, /dev/vfio, /proc and shims for sudo, systemctl, modprobe, sysctl, lsmod,
4# nvidia-smi, pgrep, curl, tee (sysfs side effects), stat, fuser, modinfo, lsinitramfs and spark-window. The
5# "35B" is a sleep process the fake start script launches. A reboot is simulated by the harness: /tmp emptied,
6# 35B gone, sysctls reset, nvidia loaded unless the hold-back file says otherwise, then the boot unit if enabled.
7# Test-only shell (no product code); evidence label: EXERCISED (host simulator). Nothing here touches the GPU,
8# the real /sys, /etc, sudo or the live 35B: window/common.sh refuses unless every acting command is a shim.
9# sh tests/window_sim.sh all scenarios
10# SIMDIR=/tmp/x sh tests/window_sim.sh keep the simulated machines in /tmp/x
11set -eu
12R=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
13BASE=${SIMDIR:-$(mktemp -d /tmp/owndriver-window-sim.XXXXXX)}
14case $BASE in /tmp/*) ;; *) echo "SIMDIR must be under /tmp"; exit 2 ;; esac
15BDF=000f:01:00.0
16pass=0
17fail=0
18
19# --- the simulated machine ------------------------------------------------------------------------------
20mkroot() {
21 S=$BASE/$1
22 rm -rf "$S"
23 mkdir -p "$S/bin" "$S/st/modes" "$S/repo" "$S/tmp" "$S/home/handoffs/OwnDriver/window" "$S/q36/bin-19173f9e" \
24 "$S/etc/sudoers.d" "$S/etc/udev/rules.d" "$S/etc/security/limits.d" "$S/etc/modprobe.d" "$S/etc/systemd/system" \
25 "$S/sys/bus/pci/devices/$BDF/power" "$S/sys/bus/pci/drivers/nvidia" "$S/sys/bus/pci/drivers/vfio-pci" \
26 "$S/sys/kernel/iommu_groups/20/devices" "$S/dev/vfio" "$S/proc" "$S/build"
27 touch "$S/.owndriver-window-sim"
28 cp -r "$R/window" "$R/tools" "$S/repo/"
29 mkdir -p "$S/repo/build"
30 ln -s "$R/build/omc" "$S/repo/build/omc"
31 D=$S/sys/bus/pci/devices/$BDF
32 echo 0x10de > "$D/vendor"; echo 0x2e12 > "$D/device"; echo "flr bus" > "$D/reset_method"
33 : > "$D/driver_override"; echo auto > "$D/power/control"; echo active > "$D/power/runtime_status"
34 ln -s ../../../../kernel/iommu_groups/20 "$D/iommu_group"
35 touch "$S/sys/kernel/iommu_groups/20/devices/$BDF" "$S/sys/bus/pci/drivers_probe" \
36 "$S/sys/bus/pci/drivers/vfio-pci/unbind" "$S/sys/bus/pci/drivers/nvidia/unbind"
37 echo "drakestapleton ALL=(root) NOPASSWD: /usr/local/sbin/aien-run-exec" > "$S/etc/sudoers.d/aien-local-ci"
38 echo "options nvidia NVreg_Example=1" > "$S/etc/modprobe.d/nvidia.conf"
39 echo simkey > "$S/q36/KEY"
40 echo 0 > "$S/st/sysctl.kernel.panic"; echo 0 > "$S/st/sysctl.kernel.panic_on_oops"
41 echo 0 > "$S/st/boots"; : > "$S/st/events"
42 printf 'window: sim\nreset: approved\nreboot: approved\n' > "$S/home/handoffs/OwnDriver/window/APPROVED"
43 case $1 in identity_*) echo "stage: W0" >> "$S/home/handoffs/OwnDriver/window/APPROVED" ;; esac
44 shims "$S"
45 cat > "$S/q36/start-35b-batch.sh" <<'EOF'
46#!/bin/sh
47# fake start-35b-batch.sh: a sleep stands in for aien-serve-http; its launch record goes to $ROOT/proc/<pid>.
48S=${OWNDRIVER_WINDOW_ROOT:?}
49[ -e "$S/st/start_fail" ] && { echo "start failed"; exit 1; }
50nohup sleep 100000 >/dev/null 2>&1 &
51p=$!
52mkdir -p "$S/proc/$p"
53printf '%s\0' ./aien-serve-http --engine omega --model-dir /home/drakestapleton/models/qwen3.6-35b-a3b-fp8-95a723d0 --port 18091 \
54 --max-context 49152 --max-tokens-cap 16384 --thinking off --batch-slots 8 > "$S/proc/$p/cmdline"
55printf 'Q36_BATCH_BIN=%s\0AIEN_Q36_STROKE=%s\0AIEN_SERVE_GPU_LOCK=%s\0AIEN_COORD_KEY=%s\0HOME=x\0' \
56 "${Q36_BATCH_BIN:-}" "${AIEN_Q36_STROKE:-}" "${AIEN_SERVE_GPU_LOCK:-}" "${AIEN_COORD_KEY:-}" > "$S/proc/$p/environ"
57ln -s "$S/q36/${Q36_BATCH_BIN:-bin-99230310}" "$S/proc/$p/cwd"
58echo aien-serve-http > "$S/proc/$p/comm"
59echo "$p" > "$S/st/35b"
60echo "start35b $p" >> "$S/st/events"
61echo "pid $p"
62EOF
63 chmod +x "$S/q36/start-35b-batch.sh"
64 cat > "$S/build/w0" <<'EOF'
65#!/bin/sh
66# fake build/w0: refuses unless the device is on vfio-pci, powered on and active, as the real one needs.
67S=${OWNDRIVER_WINDOW_ROOT:?}
68D=$S/sys/bus/pci/devices/000f:01:00.0
69echo "w0 $*" >> "$S/st/events"
70[ "$(basename "$(readlink "$D/driver")")" = vfio-pci ] || { echo "w0: not on vfio-pci"; exit 3; }
71[ "$(cat "$D/power/control")" = on ] && [ "$(cat "$D/power/runtime_status")" = active ] || { echo "w0: device not powered"; exit 4; }
72[ -e "$S/st/w0_leak" ] && echo 'SUBSYSTEM=="vfio", MODE="0666"' > "$S/etc/udev/rules.d/99-vfio.rules"
73if [ -e "$S/st/w0_crash" ]; then
74 echo crash >> "$S/st/events"; touch "$S/st/reboot"
75 pkill -KILL -f "$S/repo/window/d2-run.sh"
76 exit 9
77fi
78echo "BOOT_0 0x1b000000 BOOT_42 0x1b000000 (simulated)"
79exit "$(cat "$S/st/w0_rc" 2>/dev/null || echo 0)"
80EOF
81 chmod +x "$S/build/w0"
82 boot_power_on "$S" 0
83 start35b "$S"
84}
85
86start35b() { OWNDRIVER_WINDOW_ROOT=$1 Q36_BATCH_BIN=bin-19173f9e AIEN_Q36_STROKE=1 AIEN_SERVE_GPU_LOCK=/tmp/aien-serve-18091.lock AIEN_COORD_KEY=simkey "$1/q36/start-35b-batch.sh" >/dev/null; }
87
88bind_nvidia() {
89 S=$1; D=$S/sys/bus/pci/devices/$BDF
90 rm -f "$D/driver"; ln -s ../../drivers/nvidia "$D/driver"
91}
92
93# Power-on state; $2=1 runs the boot unit afterwards.
94boot_power_on() {
95 S=$1
96 n=$(($(cat "$S/st/boots") + 1)); echo $n > "$S/st/boots"
97 echo "boot $n" >> "$S/st/events"
98 if [ -s "$S/st/35b" ]; then kill "$(cat "$S/st/35b")" 2>/dev/null || true; rm -f "$S/st/35b"; fi
99 rm -rf "$S/tmp" && mkdir -p "$S/tmp"
100 if [ -e "$S/st/lock_after_reboot" ] && [ "$2" = 1 ]; then
101 rm -f "$S/st/lock_after_reboot"
102 (flock -x 9; sleep 20) 9>>"$S/tmp/aien-gb10.lock" &
103 echo $! > "$S/st/lockholder"; sleep 0.3
104 echo "boot: another GPU job took the chip lock" >> "$S/st/events"
105 fi
106 D=$S/sys/bus/pci/devices/$BDF
107 rm -f "$D/driver" "$S/st/modules" "$S/st/persistenced" "$S/st/nvidia_broken" "$S/dev/vfio/vfio" "$S/dev/vfio/20"
108 : > "$D/driver_override"; echo auto > "$D/power/control"
109 if [ -e "$S/st/pm_suspended" ]; then echo suspended > "$D/power/runtime_status"; else echo active > "$D/power/runtime_status"; fi
110 echo 0 > "$S/st/sysctl.kernel.panic"; echo 0 > "$S/st/sysctl.kernel.panic_on_oops"
111 bad=$(cat "$S/st/nvidia_bad_boots" 2>/dev/null || echo 0)
112 if grep -q 'install nvidia /bin/false' "$S"/etc/modprobe.d/* 2>/dev/null; then
113 echo "boot: nvidia held back" >> "$S/st/events"
114 elif [ "$bad" -gt 0 ] && [ "$2" = 1 ]; then
115 echo $((bad - 1)) > "$S/st/nvidia_bad_boots"; touch "$S/st/nvidia_broken"
116 echo "boot: nvidia failed to load (this boot)" >> "$S/st/events"
117 else
118 printf 'nvidia_uvm\nnvidia_drm\nnvidia_modeset\nnvidia\n' > "$S/st/modules"
119 bind_nvidia "$S"; touch "$S/st/persistenced"
120 fi
121 if [ "$2" = 1 ] && [ -e "$S/st/unit-enabled" ] && [ -e "$S/etc/systemd/system/aien-owndriver-window.service" ]; then
122 u=$S/etc/systemd/system/aien-owndriver-window.service
123 if ! grep -Fxq "WorkingDirectory=$S/repo" "$u" || ! grep -Fxq 'ExecStart=/bin/sh ./window/d2-boot.sh' "$u"; then
124 echo "invalid unit target" >> "$S/st/events"
125 return 1
126 fi
127 [ ! -e "$S/st/add_ogd_after_boot" ] || touch "$S/build/ogd"
128 [ ! -e "$S/st/alter_w0_after_boot" ] || echo '# changed' >> "$S/build/w0"
129 echo "unit runs" >> "$S/st/events"
130 (cd "$S/repo" && OWNDRIVER_WINDOW_ROOT=$S PATH="$S/bin:$PATH" sh "$S/repo/window/d2-boot.sh") > "$S/st/unit-$n.out" 2>&1 ||
131 echo "unit exit $?" >> "$S/st/events"
132 fi
133}
134
135# Run a window command, then follow every reboot it asks for (at most 8 boots).
136run_window() {
137 S=$1; shift
138 (cd "$S/repo" && OWNDRIVER_WINDOW_ROOT=$S PATH="$S/bin:$PATH" "$@") > "$S/st/cmd.out" 2>&1 && echo "cmd exit 0" >> "$S/st/events" ||
139 echo "cmd exit $?" >> "$S/st/events"
140 while [ -e "$S/st/reboot" ]; do
141 rm -f "$S/st/reboot"
142 [ "$(cat "$S/st/boots")" -lt 9 ] || { echo "boot loop" >> "$S/st/events"; return; }
143 boot_power_on "$S" 1
144 done
145}
146
147shims() {
148 S=$1; B=$S/bin
149 w() { printf '#!/bin/sh\nS=%s\nD=$S/sys/bus/pci/devices/%s\n%s\n' "$S" "$BDF" "$2" > "$B/$1"; chmod +x "$B/$1"; }
150 w ss '[ ! -e "$S/st/ss_fail" ] || exit 1
151[ ! -e "$S/st/unrecognized" ] || echo "LISTEN 0 1 127.0.0.1:18091"
152exit 0'
153 w sudo '[ "$1" = -n ] && shift
154exec "$@"'
155 w systemctl 'echo "systemctl $*" >> $S/st/events
156case $1 in
157daemon-reload) ;;
158enable) [ -e $S/st/fail_enable ] && exit 1; [ -e $S/etc/systemd/system/$2 ] && touch $S/st/unit-enabled ;;
159disable) rm -f $S/st/unit-enabled ;;
160is-active) [ "$2" = --quiet ] && shift; [ -e $S/st/persistenced ] || { [ "$2" = --quiet ] || echo inactive; exit 3; }; echo active ;;
161start) [ -s $S/st/modules ] && touch $S/st/persistenced ;;
162reboot) touch $S/st/reboot ;;
163*) exit 1 ;;
164esac'
165 w modprobe 'echo "modprobe $*" >> $S/st/events
166for m in "$@"; do case $m in
167vfio-pci) grep -qx vfio_pci $S/st/modules 2>/dev/null || echo vfio_pci >> $S/st/modules; : > $S/dev/vfio/vfio; echo root:root:666 > $S/st/modes/vfio ;;
168nvidia*) grep -q "install nvidia /bin/false" $S/etc/modprobe.d/* 2>/dev/null && exit 1
169 [ -e $S/st/nvidia_broken ] && exit 1
170 for n in nvidia nvidia_modeset nvidia_drm nvidia_uvm; do grep -qx $n $S/st/modules 2>/dev/null || echo $n >> $S/st/modules; done
171 [ -e $D/driver ] || [ -n "$(cat $D/driver_override)" ] || { ln -s ../../drivers/nvidia $D/driver; } ;;
172*) exit 1 ;;
173esac; done'
174 w lsmod 'echo "Module Size Used by"
175[ -s $S/st/modules ] && while read -r m; do echo "$m 16384 0"; done < $S/st/modules
176exit 0'
177 w sysctl 'case $1 in
178-n) cat $S/st/sysctl.$2 ;;
179-q) shift; if [ "$1" = -p ]; then while IFS== read -r k v; do echo "$v" > $S/st/sysctl.$k; done < "$2"; else for a; do echo "${a#*=}" > $S/st/sysctl.${a%%=*}; done; fi
180 echo "sysctl $*" >> $S/st/events ;;
181*) exit 1 ;;
182esac'
183 w nvidia-smi 'up() { [ -s $S/st/35b ] && kill -0 "$(cat $S/st/35b)" 2>/dev/null; }
184case $1 in
185-L) grep -qx nvidia $S/st/modules 2>/dev/null && [ "$(basename "$(readlink $D/driver)")" = nvidia ] && echo "GPU 0: NVIDIA GB10 (UUID: GPU-sim)" || { echo "No devices were found"; exit 6; } ;;
186--query-compute-apps=pid) up && cat $S/st/35b; exit 0 ;;
187--query-compute-apps=process_name,used_memory) up && echo "aien-serve-http, 43520 MiB"; exit 0 ;;
188*) exit 1 ;;
189esac'
190 w pgrep '[ -s $S/st/35b ] && kill -0 "$(cat $S/st/35b)" 2>/dev/null && cat $S/st/35b && exit 0
191exit 1'
192 w curl 'up() { [ -s $S/st/35b ] && kill -0 "$(cat $S/st/35b)" 2>/dev/null && [ ! -e $S/st/serve_dead ]; }
193up || exit 7
194case "$*" in
195*/v1/models*) echo "{\"data\":[{\"id\":\"qwen3.6-35b-a3b\"}]}" ;;
196*/v1/chat/completions*) echo "{\"choices\":[{\"message\":{\"content\":\"ready\"}}]}" ;;
197*) exit 22 ;;
198esac'
199 w tee '/usr/bin/tee "$@" || exit $?
200for a; do t=$a; done
201case $t in
202$S/sys/bus/pci/drivers_probe)
203 if [ ! -e $D/driver ]; then
204 o=$(cat $D/driver_override)
205 if [ "$o" = vfio-pci ] && grep -qx vfio_pci $S/st/modules; then ln -s ../../drivers/vfio-pci $D/driver; : > $S/dev/vfio/20; cat $S/st/vfio_mode 2>/dev/null > $S/st/modes/20 || echo root:root:600 > $S/st/modes/20; echo "bind vfio-pci" >> $S/st/events
206 elif [ -z "$o" ] && grep -qx nvidia $S/st/modules 2>/dev/null; then ln -s ../../drivers/nvidia $D/driver; echo "bind nvidia" >> $S/st/events; fi
207 fi ;;
208$S/sys/bus/pci/drivers/vfio-pci/unbind) rm -f $D/driver $S/dev/vfio/20; echo "unbind vfio-pci (FLR)" >> $S/st/events ;;
209$D/power/control) [ "$(cat $D/power/control)" = on ] && [ ! -e $S/st/pm_stuck ] && echo active > $D/power/runtime_status ;;
210esac
211exit 0'
212 w stat 'for a; do f=$a; done
213case $f in $S/dev/vfio/*) cat $S/st/modes/$(basename $f) ;; *) exec /usr/bin/stat "$@" ;; esac'
214 w fuser 'echo " 1755 $(cat $S/st/35b 2>/dev/null)"'
215 w modinfo 'case "$*" in
216"-n vfio-pci") echo /lib/modules/sim/vfio-pci.ko ;;
217nvgrace-gpu-vfio-pci) for a in 31C2 2941 2348 2345 2342; do echo "alias: pci:v000010DEd0000${a}sv*sd*bc*sc*i*"; done ;;
218*) exit 1 ;;
219esac'
220 w lsinitramfs '[ ! -e "$S/st/initramfs_fail" ] || exit 1
221echo usr/lib/modules/sim/kernel/drivers/vfio/pci/vfio-pci.ko'
222 w spark-window 'echo free'
223}
224
225# --- assertions ----------------------------------------------------------------------------------------
226ok() { pass=$((pass + 1)); echo " ok $1"; }
227bad() { fail=$((fail + 1)); echo " FAIL $1"; FAILED_IN="$FAILED_IN $CASE"; }
228check() { if eval "$2"; then ok "$1"; else bad "$1"; fi; }
229L() { echo "$S/home/handoffs/OwnDriver/window"; }
230alive35b() { [ -s "$S/st/35b" ] && kill -0 "$(cat "$S/st/35b")" 2>/dev/null; }
231restored() {
232 check "35B running again" alive35b
233 check "35B restarted with the recorded env (key compared)" "tr '\\0' '\\n' < $S/proc/\$(cat $S/st/35b)/environ | grep -qx AIEN_COORD_KEY=simkey && tr '\\0' '\\n' < $S/proc/\$(cat $S/st/35b)/environ | grep -qx Q36_BATCH_BIN=bin-19173f9e"
234 check "nvidia bound, 4 modules" "[ \$(basename \$(readlink $S/sys/bus/pci/devices/$BDF/driver)) = nvidia ] && [ \$(grep -c nvidia $S/st/modules) = 4 ]"
235 check "hold-back file and unit gone" "[ ! -e $S/etc/modprobe.d/zz-aien-owndriver-window.conf ] && [ ! -e $S/etc/systemd/system/aien-owndriver-window.service ] && [ ! -e $S/st/unit-enabled ]"
236 check "sysctl back to 0/0" "[ \$(cat $S/st/sysctl.kernel.panic) = 0 ] && [ \$(cat $S/st/sysctl.kernel.panic_on_oops) = 0 ]"
237 check "state, approval, restore-path cleared" "[ ! -e $(L)/state ] && [ ! -e $(L)/APPROVED ] && [ ! -e $(L)/restore-path ]"
238 check "driver_override empty" "[ -z \"\$(tr -d '\\n' < $S/sys/bus/pci/devices/$BDF/driver_override)\" ]"
239 check "BOARD says WINDOW CLOSED" "grep -q 'WINDOW CLOSED' $S/home/handoffs/BOARD.md"
240 check "power/control back to auto" "[ \$(cat $S/sys/bus/pci/devices/$BDF/power/control) = auto ]"
241}
242untouched() {
243 check "35B never stopped" "alive35b && ! grep -q 'boot 2' $S/st/events"
244 check "no window file written" "[ ! -e $S/etc/modprobe.d/zz-aien-owndriver-window.conf ] && [ ! -e $S/etc/systemd/system/aien-owndriver-window.service ] && [ ! -e $(L)/state ]"
245}
246boots() { check "boots after power-on: $1" "[ \$(cat $S/st/boots) = $(($1 + 1)) ]"; }
247
248FAILED_IN=
249CASE=w0; echo "case $CASE: W0 read-only"; mkroot $CASE
250run_window "$S" sh window/d2-pre.sh
251check "W0 exit 0" "grep -qx 'cmd exit 0' $S/st/events"; untouched
252check "baseline written" "[ -s $(L)/priv-baseline ]"
253
254# mkfw S: a build/fw in fwprep's manifest format (fw_file names the real firmware sha256).
255mkfw() {
256 mkdir -p "$1/build/fw"
257 {
258 echo "fwmanifest 1"
259 echo "fw_file bytes=75012080 sha256=32cb7cf3e77f97e92f6e92135939ccb3ba382748144569c0dfccf3338d43d963"
260 for o in fmc.img fmc.hash fmc.pubkey fmc.sig bl.img; do
261 echo "sim $o" > "$1/build/fw/$o"
262 echo "out $o bytes=$(wc -c < "$1/build/fw/$o") sha256=$(sha256sum "$1/build/fw/$o" | cut -c1-64)"
263 done
264 echo end
265 } > "$1/build/fw/fw.manifest.tmp"
266 mv "$1/build/fw/fw.manifest.tmp" "$1/build/fw/fw.manifest"
267}
268CASE=fwgood; echo "case $CASE: W0 with a matching build/fw"; mkroot $CASE
269mkfw "$S"
270run_window "$S" sh window/d2-pre.sh
271check "W0 exit 0" "grep -qx 'cmd exit 0' $S/st/events"; untouched
272check "build/fw checked" "grep -q 'build/fw matches fw.manifest' $(L)/*-pre.log"
273
274CASE=fwbad; echo "case $CASE: W0 with a build/fw file that differs from fw.manifest"; mkroot $CASE
275mkfw "$S"; echo tampered > "$S/build/fw/bl.img"
276run_window "$S" sh window/d2-pre.sh
277check "W0 refused" "grep -qx 'cmd exit 1' $S/st/events"; untouched
278check "names the mismatch" "grep -q 'build/fw files do not match' $(L)/*-pre.log"
279
280CASE=d2; echo "case $CASE: full D2 window, reboot restore path"; mkroot $CASE
281run_window "$S" sh window/d2-pre.sh --go D2
282boots 2; restored
283check "W2 bound vfio-pci, then W7 unbound it (FLR)" "grep -q 'bind vfio-pci' $S/st/events && grep -q 'unbind vfio-pci' $S/st/events"
284check "w0 ran on the device" "grep -q '^w0 000f:01:00.0 20' $S/st/events"
285check "W4/W5 logged NOT_RUN (no build/ogd)" "grep -q \"W-boot NOT_RUN\" $(L)/*-run.log && grep -q \"W-hello NOT_RUN\" $(L)/*-run.log"
286check "panic sysctls were 10/1 during W2" "grep -q 'sysctl kernel.panic=10 kernel.panic_on_oops=1' $S/st/events"
287
288CASE=identity_up; echo "case $CASE"; mkroot "$CASE"
289run_window "$S" sh window/d2-pre.sh --go W0
290boots 2; restored
291check "identity ran exactly once" "test \$(grep -c '^w0 ' $S/st/events) = 1"
292check "cleared identity receipts" "[ ! -e $(L)/identity-service ] && [ ! -e $(L)/identity-executable.sha256 ]"
293
294stop_sim_service() { kill "$(cat "$S/st/35b")"; rm -f "$S/st/35b"; }
295CASE=identity_down; echo "case $CASE"; mkroot "$CASE"; stop_sim_service
296run_window "$S" sh window/d2-pre.sh --go W0
297boots 2
298check "previously idle port kept idle" "! alive35b && test \$(grep -c '^start35b ' $S/st/events) = 1 && grep -q 'WINDOW CLOSED' $S/home/handoffs/BOARD.md"
299
300CASE=identity_foreign_service; echo "case $CASE"; mkroot "$CASE"
301printf '%s\0' test-foreign-argument >> "$S/proc/$(cat "$S/st/35b")/cmdline"
302run_window "$S" sh window/d2-pre.sh --go W0
303boots 0; untouched
304check "foreign command line kept out of logs" "! grep -rqs test-foreign-argument $(L) && grep -q 'another launch recipe' $(L)/*-pre.log"
305
306CASE=identity_downgrade; echo "case $CASE"; mkroot "$CASE"
307run_window "$S" sh window/d2-pre.sh --go D2
308boots 0; untouched
309check "identity authorization refuses broader stage" "grep -q 'approval does not name this stage' $(L)/*-pre.log"
310
311for refusal in initramfs_fail ss_fail unrecognized ogd; do
312 CASE=identity_$refusal; echo "case $CASE"; mkroot "$CASE"; stop_sim_service
313 if [ "$refusal" = ogd ]; then touch "$S/build/ogd"; else touch "$S/st/$refusal"; fi
314 run_window "$S" sh window/d2-pre.sh --go W0
315 boots 0
316 check "failed before arming the window" "grep -q 'cmd exit 1' $S/st/events && [ ! -e $(L)/state ] && [ ! -e $S/st/unit-enabled ]"
317done
318
319for mutation in add_ogd_after_boot alter_w0_after_boot; do
320 CASE=identity_$mutation; echo "case $CASE"; mkroot "$CASE"; touch "$S/st/$mutation"
321 run_window "$S" sh window/d2-pre.sh --go W0
322 boots 2; restored
323 check "post-boot mutation refused before binding" "! grep -q '^bind vfio-pci' $S/st/events && ! grep -q '^w0 ' $S/st/events"
324done
325
326CASE=identity_restore_failure; echo "case $CASE"; mkroot "$CASE"; echo 1 > "$S/st/nvidia_bad_boots"
327run_window "$S" sh window/d2-pre.sh --go W0
328boots 2
329check "failed restore requests attended recovery" "grep -q 'Attended recovery required' $(L)/*-restore.log && test \"\$(cat $(L)/state)\" = 'restore W0'"
330
331CASE=rebind; echo "case $CASE: D2 window, RESTORE_PATH=rebind"; mkroot $CASE
332run_window "$S" env RESTORE_PATH=rebind sh window/d2-pre.sh --go D2
333boots 1; restored
334
335CASE=noappr; echo "case $CASE: no approval file"; mkroot $CASE
336rm "$(L)/APPROVED"
337run_window "$S" sh window/d2-pre.sh --go D2
338check "refused" "grep -q 'cmd exit 1' $S/st/events"; untouched
339
340CASE=noreset; echo "case $CASE: approval without 'reset: approved'"; mkroot $CASE
341printf 'window: sim\nreboot: approved\n' > "$(L)/APPROVED"
342run_window "$S" sh window/d2-pre.sh --go D2
343check "refused" "grep -q 'cmd exit 1' $S/st/events"; untouched
344
345CASE=locked; echo "case $CASE: chip lock held by another GPU job"; mkroot $CASE
346(flock -x 9; sleep 30) 9>>"$S/tmp/aien-gb10.lock" &
347lockpid=$!; sleep 0.3
348run_window "$S" sh window/d2-pre.sh --go D2
349kill $lockpid 2>/dev/null || true
350check "refused" "grep -q 'cmd exit 1' $S/st/events"; untouched
351
352CASE=snapdiff; echo "case $CASE: live 35B started differently from the restore recipe"; mkroot $CASE
353printf 'Q36_BATCH_BIN=bin-99230310\0AIEN_COORD_KEY=simkey\0' > "$S/proc/$(cat "$S/st/35b")/environ"
354run_window "$S" sh window/d2-pre.sh --go D2
355check "refused" "grep -q 'cmd exit 1' $S/st/events"; untouched
356
357CASE=w1abort; echo "case $CASE: W1 fails after stopping the 35B (systemctl enable fails)"; mkroot $CASE
358touch "$S/st/fail_enable"
359run_window "$S" sh window/d2-pre.sh --go D2
360boots 0; restored
361check "W1 exit nonzero" "grep -q 'cmd exit 1' $S/st/events"
362
363CASE=pm; echo "case $CASE: runtime PM stays suspended after power/control=on"; mkroot $CASE
364touch "$S/st/pm_suspended" "$S/st/pm_stuck"
365run_window "$S" sh window/d2-pre.sh --go D2
366rm -f "$S/st/pm_suspended" "$S/st/pm_stuck"
367check "w0 never ran (no BAR access while suspended)" "! grep -q '^w0 ' $S/st/events"
368check "W3 refused on runtime_status" "grep -q 'runtime_status=suspended, not active' $(L)/*-run.log"
369boots 2; restored
370
371CASE=w0fail; echo "case $CASE: w0 fails"; mkroot $CASE
372echo 3 > "$S/st/w0_rc"
373run_window "$S" sh window/d2-pre.sh --go D2
374boots 2; restored
375check "release logged rc=1" "grep -q 'W7 release (rc=1)' $(L)/*-run.log"
376
377CASE=crash; echo "case $CASE: crash during W3 (no trap runs)"; mkroot $CASE
378touch "$S/st/w0_crash"
379run_window "$S" sh window/d2-pre.sh --go D2
380boots 2; restored
381
382CASE=fallback; echo "case $CASE: nvidia fails to come back once: one reboot fallback"; mkroot $CASE
383echo 1 > "$S/st/nvidia_bad_boots"
384run_window "$S" sh window/d2-pre.sh --go D2
385boots 3; restored
386check "fallback used once" "grep -q 'reboot fallback' $S/home/handoffs/BOARD.md"
387
388CASE=fallback2; echo "case $CASE: nvidia never comes back: stop after one fallback"; mkroot $CASE
389echo 99 > "$S/st/nvidia_bad_boots"
390run_window "$S" sh window/d2-pre.sh --go D2
391boots 3
392check "BOARD says restore FAILED" "grep -q 'restore FAILED after the reboot fallback' $S/home/handoffs/BOARD.md"
393check "no reboot loop" "! grep -q 'boot loop' $S/st/events"
394check "unit and state kept for the next (manual) boot" "[ -e $S/etc/systemd/system/aien-owndriver-window.service ] && [ \$(cat $(L)/state) = restore ]"
395
396CASE=leak; echo "case $CASE: a privilege file appears during the window"; mkroot $CASE
397touch "$S/st/w0_leak"
398run_window "$S" sh window/d2-pre.sh --go D2
399check "BOARD says PRIVILEGE PERSISTED" "grep -q 'PRIVILEGE PERSISTED' $S/home/handoffs/BOARD.md"
400check "no WINDOW CLOSED" "! grep -q 'WINDOW CLOSED' $S/home/handoffs/BOARD.md"
401
402CASE=vfiomode; echo "case $CASE: /dev/vfio/20 comes up world-writable"; mkroot $CASE
403echo root:root:666 > "$S/st/vfio_mode"
404run_window "$S" sh window/d2-pre.sh --go D2
405check "w0 never ran" "! grep -q '^w0 ' $S/st/events"
406check "W2 refused on the mode" "grep -q 'is root:root:666, expected root:root:600' $(L)/*-run.log"
407boots 2; restored
408
409CASE=lockboot; echo "case $CASE: another GPU job takes the chip lock right after the W1 reboot"; mkroot $CASE
410touch "$S/st/lock_after_reboot"
411run_window "$S" sh window/d2-pre.sh --go D2
412kill "$(cat "$S/st/lockholder")" 2>/dev/null || true
413check "w0 never ran" "! grep -q '^w0 ' $S/st/events"
414check "W2 refused on the lock" "grep -q 'is held: something started a GPU job' $(L)/*-run.log"
415boots 2; restored
416
417CASE=d3noogd; echo "case $CASE: D3 without build/ogd"; mkroot $CASE
418D3_CMD="make chip-gates" run_window "$S" sh window/d2-pre.sh --go D3
419check "refused" "grep -q 'cmd exit 1' $S/st/events"; untouched
420
421for capability in model suffix failed; do
422 CASE=d3$capability; echo "case $CASE: D3 requires successful exact exec capability"; mkroot "$CASE"
423 before=$(cat "$S/st/35b")
424 case $capability in model) advertised='model\nmodel-trace'; queryrc=0 ;; suffix) advertised=exec-extra; queryrc=0 ;; failed) advertised=exec; queryrc=1 ;; esac
425 printf '#!/bin/sh\n[ "$1" = modes ] || exit 95\nprintf "%%b\\n" "%s"\nexit %s\n' "$advertised" "$queryrc" > "$S/build/ogd"
426 chmod +x "$S/build/ogd"
427 D3_CMD="make chip-gates" run_window "$S" sh window/d2-pre.sh --go D3
428 check "D3 capability refused before disruption" "grep -q 'cmd exit 1' $S/st/events && grep -q \"successful exact 'exec' mode\" $(L)/*-pre.log"
429 untouched; boots 0
430 check "original server retained" "[ '$before' = \"\$(cat $S/st/35b)\" ] && kill -0 '$before'"
431 check "no VFIO binding" "! grep -q 'bind vfio-pci' $S/st/events"
432 check "no D3 command or restore-path persisted" "[ ! -e $(L)/d3-cmd ] && [ ! -e $(L)/restore-path ]"
433done
434
435CASE=d3exec; echo "case $CASE: successful exact exec capability reaches D3"; mkroot "$CASE"
436cat > "$S/build/ogd" <<'EOF'
437#!/bin/sh
438case $1 in
439modes) echo exec ;;
440exec) echo "ogd exec" >> "${OWNDRIVER_WINDOW_ROOT:?}/st/events" ;;
441*) exit 95 ;;
442esac
443EOF
444chmod +x "$S/build/ogd"
445D3_CMD="make chip-gates" run_window "$S" sh window/d2-pre.sh --go D3
446check "D3 exec ran in isolated simulator" "grep -q '^ogd exec$' $S/st/events"
447boots 2; restored
448
449CASE=stale; echo "case $CASE: boot unit left over with no state"; mkroot $CASE
450sed "s|@CHECKOUT@|$S/repo|g" "$R/window/aien-owndriver-window.service" > "$S/etc/systemd/system/aien-owndriver-window.service"; touch "$S/st/unit-enabled"
451boot_power_on "$S" 1
452check "unit removed itself" "[ ! -e $S/etc/systemd/system/aien-owndriver-window.service ] && [ ! -e $S/st/unit-enabled ]"
453check "no driver change" "! grep -q 'bind vfio-pci' $S/st/events"
454
455for s in "$BASE"/*/st/35b; do [ -s "$s" ] && kill "$(cat "$s")" 2>/dev/null || true; done
456[ -n "${SIMDIR:-}" ] || rm -rf "$BASE"
457if [ "$fail" -eq 0 ]; then
458 echo "window_sim: PASS ($pass checks, $(grep -c '^CASE=' "$0") scenarios)"
459else
460 echo "window_sim: FAIL ($fail of $((pass + fail)) checks; in:$FAILED_IN)"
461 exit 1
462fi