#!/bin/sh
# DF-5641 — live demonstration that isa_alloc_resource() executes with a
# type-confused child-ivars cast (DEVTOISA) on passthrough, and survives
# only because resource_list_alloc() early-forwards (subr_bus.c:2683-2687).
#
# Host-side driver around dfbsd-qemu/vm.sh. Run from the repo root.
# Prereq: guest up (vm.sh up). Leaves the guest RESTORED at exit.

set -e
VM=dfbsd-qemu/vm.sh
HINTS='hint.atkbdc.0.at="isa"
hint.atkbdc.0.port="0x60"
hint.atkbdc.0.irq="1"
hint.psm.0.at="atkbdc0"
hint.psm.0.irq="12"'

echo "== guest baseline =="
$VM run_root 'uname -a'
$VM run_root 'devinfo | grep -A2 atkbdc0 || true'

echo "== install hints (atkbdc0 on the ISA bus -> psm0 becomes an isa grandchild) =="
$VM run_root "printf '%s\n' '$HINTS' >> /boot/loader.conf.local; cat /boot/loader.conf.local; shutdown -r now" || true
sleep 110
$VM status

echo "== observed: passthrough chain executed, guest alive =="
$VM run_root 'devinfo | grep -B4 -A2 psm0'
$VM run_root 'dmesg | grep -E "atkbdc0|psm0" | tail -6'
$VM run_root 'vmstat -i | grep -E "psm0|atkbd0"'   # psm0 registered => alloc chain OK

echo "== restore =="
$VM run_root 'truncate -s0 /boot/loader.conf.local; shutdown -r now' || true
sleep 110
$VM run_root 'devinfo | grep -c acpi0'             # ACPI config back
echo "Expected: devinfo tree shows isa0 -> atkbdc0 -> psm0, psm0 in vmstat -i,"
echo "no panic; guest restored afterwards (acpi0 present again)."
