RustForge

rust-kernel

Rustbelt: a hobby x86_64 kernel written in Rust

rust-kernel / scripts / run-headless.sh
1307 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash
# Build the kernel with the `headless` feature and boot it in QEMU with no
# window. Serial output goes to this terminal; the kernel powers QEMU off via
# the isa-debug-exit device when done. A timeout guards against hangs.
#
# Usage: ./scripts/run-headless.sh                 # normal boot, exits after ~3 s of timer ticks
#        ./scripts/run-headless.sh stack-overflow  # extra cargo features, comma separated
set -euo pipefail
cd "$(dirname "$0")/.."

cargo build --features "headless${1:+,$1}"
# Force bootimage to repack the disk image (see scripts/runner.sh for why).
touch target/x86_64-rust_kernel/debug/rust_kernel
cargo bootimage --features "headless${1:+,$1}"
IMG=target/x86_64-rust_kernel/debug/bootimage-rust_kernel.bin

set +e
timeout 30 qemu-system-x86_64 \
    -drive format=raw,file="$IMG" \
    -device isa-debug-exit,iobase=0xf4,iosize=0x04 \
    -serial stdio -display none -no-reboot
code=$?
set -e

# isa-debug-exit: QEMU exit status = (value << 1) | 1, so Success(0x10) -> 33.
case $code in
    33)  echo "[run-headless] kernel exited OK"; exit 0 ;;
    35)  echo "[run-headless] kernel reported FAILURE"; exit 1 ;;
    124) echo "[run-headless] timed out (kernel hung?)"; exit 124 ;;
    *)   echo "[run-headless] QEMU exited with $code"; exit "$code" ;;
esac