Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/physics-mv/blob/main/tools/qemu_inspect.sh # physics-mv FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `physics-mv / tools / qemu_inspect.sh` 169 lines ยท 6309 bytes Raw Blame History 1 #!/bin/sh 2 # m3/tools/qemu_inspect.sh -- Python-free QEMU state inspection for M3 gates. 3 # 4 # Boots qemu-system-aarch64 (-M virt -cpu cortex-a57 -m 128M, TCG single 5 # thread), waits for a UART marker in the serial file, stops the guest, dumps 6 # named guest-physical regions with QMP pmemsave, then quits. Fails closed: 7 # exit nonzero on timeout, fail-marker, QMP error, or any short/missing dump. 8 # 9 # Usage: 10 # qemu_inspect.sh --out DIR --marker ERE [--fail-marker ERE] [--timeout SECS] 11 # [--bios FILE] [--loader FILE@ADDR]... [--region NAME:ADDR:LEN]... 12 # [--qemu-arg ARG]... 13 # ADDR/LEN accept 0x hex or decimal. Region dumps land in DIR/NAME.bin. 14 # Regions must lie inside guest RAM [0x40000000, +QEMU_MEM): pmemsave of 15 # unmapped space returns zeros silently, so it is refused up front. 16 # Env overrides: QEMU (binary), QEMU_MACHINE, QEMU_CPU, QEMU_MEM. 17 # Outputs in DIR: serial.txt, qmp.log, qemu-cmd.txt, qemu-version.txt, NAME.bin. 18 # Exit codes: 0 ok, 1 usage/setup/QMP/dump failure, 2 marker timeout, 19 # 3 fail-marker seen, 4 QEMU exited before the marker. 20 set -eu 21 22 QEMU=${QEMU:-qemu-system-aarch64} 23 QEMU_MACHINE=${QEMU_MACHINE:-virt} 24 QEMU_CPU=${QEMU_CPU:-cortex-a57} 25 QEMU_MEM=${QEMU_MEM:-128M} 26 27 die() { echo "qemu_inspect: $*" >&2; exit "${RC:-1}"; } 28 29 OUT= MARKER= FAIL_MARKER= TIMEOUT=10 BIOS= 30 LOADERS= REGIONS= EXTRA= 31 nl=' 32 ' 33 while [ $# -gt 0 ]; do 34 case $1 in 35 --out) OUT=$2; shift 2 ;; 36 --marker) MARKER=$2; shift 2 ;; 37 --fail-marker) FAIL_MARKER=$2; shift 2 ;; 38 --timeout) TIMEOUT=$2; shift 2 ;; 39 --bios) BIOS=$2; shift 2 ;; 40 --loader) LOADERS="$LOADERS$2$nl"; shift 2 ;; 41 --region) REGIONS="$REGIONS$2$nl"; shift 2 ;; 42 --qemu-arg) EXTRA="$EXTRA$2$nl"; shift 2 ;; 43 *) die "unknown argument: $1" ;; 44 esac 45 done 46 [ -n "$OUT" ] || die "--out required" 47 [ -n "$MARKER" ] || die "--marker required" 48 case $TIMEOUT in ''|*[!0-9]*) die "--timeout must be whole seconds" ;; esac 49 [ "$TIMEOUT" -ge 1 ] || die "--timeout must be >= 1" 50 command -v "$QEMU" >/dev/null || die "$QEMU not found" 51 command -v socat >/dev/null || die "socat not found" 52 53 to_dec() { # strict unsigned number -> decimal; empty on error 54 case $1 in 55 0x*|0X*) h=${1#0?}; case $h in ''|*[!0-9a-fA-F]*) return 1 ;; esac ;; 56 *) case $1 in ''|*[!0-9]*) return 1 ;; esac ;; 57 esac 58 printf '%d' "$1" 59 } 60 61 umask 077 62 [ ! -L "$OUT" ] || die "output directory must not be a symlink" 63 mkdir -p "$OUT" 64 OUT=$(cd "$OUT" && pwd -P) 65 [ "$(stat -c %u "$OUT")" = "$(id -u)" ] || die "output directory is not owned by caller" 66 [ -z "$(find "$OUT" -maxdepth 0 -perm /077 -print)" ] || die "output directory is not private" 67 SOCK=$OUT/qmp.sock 68 rm -f "$OUT/serial.txt" "$OUT/qmp.log" "$SOCK" 69 70 # Regions must lie wholly inside guest RAM: QEMU pmemsave of unmapped space 71 # silently returns zeros, which would fake "no effect" evidence. 72 RAM_BASE=$((0x40000000)) 73 case $QEMU_MEM in 74 *[!0-9]*[MG]|[MG]) die "QEMU_MEM must be NNM or NNG: $QEMU_MEM" ;; 75 *M) RAM_SIZE=$(( ${QEMU_MEM%M} * `1048576` )) ;; 76 *G) RAM_SIZE=$(( ${QEMU_MEM%G} * `1073741824` )) ;; 77 *) die "QEMU_MEM must be NNM or NNG: $QEMU_MEM" ;; 78 esac 79 RAM_END=$((RAM_BASE + RAM_SIZE)) 80 81 # Validate regions up front; remove stale dumps. 82 IFS_SAVE=$IFS 83 IFS=$nl 84 for r in $REGIONS; do 85 IFS=: read -r name addr len </dev/null || die "bad region addr in '$r'" 90 to_dec "$len" >/dev/null || die "bad region len in '$r'" 91 a=$(to_dec "$addr"); n=$(to_dec "$len") 92 [ "$n" -gt 0 ] || die "zero-length region '$r'" 93 [ "$a" -ge "$RAM_BASE" ] && [ "$n" -le "$RAM_SIZE" ] && [ "$a" -le $((RAM_END - n)) ] \ 94 || die "region '$r' is not inside guest RAM [0x40000000, +$QEMU_MEM)" 95 rm -f "$OUT/$name.bin" 96 done 97 98 # Build the QEMU argument list as positional parameters. 99 set -- "$QEMU" -M "$QEMU_MACHINE" -cpu "$QEMU_CPU" -m "$QEMU_MEM" \ 100 -accel tcg,thread=single -display none -monitor none \ 101 -serial "file:$OUT/serial.txt" -qmp "unix:$SOCK,server=on,wait=off" 102 [ -z "$BIOS" ] || { [ -f "$BIOS" ] || die "bios not found: $BIOS"; set -- "$@" -bios "$BIOS"; } 103 for l in $LOADERS; do 104 f=${l%@*}; a=${l##*@} 105 [ "$f" != "$l" ] || die "loader must be FILE@ADDR: $l" 106 [ -f "$f" ] || die "loader file not found: $f" 107 to_dec "$a" >/dev/null || die "bad loader addr: $l" 108 set -- "$@" -device "loader,file=$f,addr=$a,force-raw=on" 109 done 110 for e in $EXTRA; do set -- "$@" "$e"; done 111 IFS=$IFS_SAVE 112 113 "$QEMU" --version > "$OUT/qemu-version.txt" 114 { for a in "$@"; do printf "'%s' " "$a"; done; echo; } > "$OUT/qemu-cmd.txt" 115 116 "$@" & 117 QPID=$! 118 cleanup() { kill "$QPID" 2>/dev/null || true; wait "$QPID" 2>/dev/null || true; rm -f "$SOCK"; } 119 trap cleanup EXIT INT TERM 120 121 # Wait for the marker (bounded; 50 ms steps). 122 steps=$((TIMEOUT * 20)); i=0 123 while :; do 124 if [ -n "$FAIL_MARKER" ] && grep -qE "$FAIL_MARKER" "$OUT/serial.txt" 2>/dev/null; then 125 RC=3 die "fail marker /$FAIL_MARKER/ seen in serial output" 126 fi 127 grep -qE "$MARKER" "$OUT/serial.txt" 2>/dev/null && break 128 kill -0 "$QPID" 2>/dev/null || RC=4 die "QEMU exited before marker /$MARKER/" 129 i=$((i + 1)) 130 [ "$i" -le "$steps" ] || RC=2 die "timeout (${TIMEOUT}s) waiting for marker /$MARKER/" 131 sleep 0.05 132 done 133 [ -S "$SOCK" ] || die "QMP socket missing: $SOCK" 134 135 # One QMP session: capabilities, stop, pmemsave each region, quit. 136 n=0 137 { 138 printf '{"execute":"qmp_capabilities"}\n' 139 printf '{"execute":"stop"}\n' 140 IFS=$nl 141 for r in $REGIONS; do 142 IFS=: read -r name addr len < "$OUT/qmp.log" 2>"$OUT/socat.err" || true 151 IFS=$IFS_SAVE 152 wait "$QPID" 2>/dev/null || true 153 trap - EXIT INT TERM 154 rm -f "$SOCK" 155 156 grep -q '"error"' "$OUT/qmp.log" && die "QMP error (see $OUT/qmp.log)" 157 IFS=$nl 158 for r in $REGIONS; do 159 IFS=: read -r name addr len < $OUT"