1#!/usr/bin/env bash
2
3DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4
5function HELP {
6    echo "help:"
7    echo "-b a virtio block device"
8    echo "-c cmpctmalloc instead of dlmalloc"
9    echo "-M miniheap instead of dlmalloc"
10    echo "-n a virtio network device"
11    echo "-t a virtio tap network device"
12    echo "-d a virtio display"
13    echo "-e sifive e platform"
14    echo "-u sifive u platform"
15    echo "-6 64bit"
16    echo "-S supervisor mode (using OpenSBI)"
17    echo "   currently only works in 64bit mode"
18    echo "-m <memory in MB>"
19    echo "-s <number of cpus>"
20    echo "-h for help"
21    echo "all arguments after -- are passed to qemu directly"
22    exit 1
23}
24
25DO_NET=0
26DO_NET_TAP=0
27DO_BLOCK=0
28DO_64BIT=0
29DO_EMBEDDED=0
30DO_UNLEASHED=0
31DO_DISPLAY=0
32DO_CMPCTMALLOC=0
33DO_MINIHEAP=0
34DO_SUPERVISOR=0
35SMP=1
36MEMSIZE=512
37SUDO=""
38PROJECT=""
39
40while getopts bdhm:cMmnteu6p:s:S FLAG; do
41    case $FLAG in
42        b) DO_BLOCK=1;;
43        c) DO_CMPCTMALLOC=1;;
44        d) DO_DISPLAY=1;;
45        M) DO_MINIHEAP=1;;
46        n) DO_NET=1;;
47        t) DO_NET_TAP=1;;
48        e) DO_EMBEDDED=1;;
49        u) DO_UNLEASHED=1;;
50        6) DO_64BIT=1;;
51        m) MEMSIZE=$OPTARG;;
52        s) SMP=$OPTARG;;
53        S) DO_SUPERVISOR=1;;
54        p) PROJECT=$OPTARG;;
55        h) HELP;;
56        \?)
57            echo unrecognized option
58            HELP
59    esac
60done
61
62shift $((OPTIND-1))
63
64CPU=""
65BIOS=""
66
67if (( $DO_UNLEASHED == 1 )); then
68    QEMU="qemu-system-riscv64"
69    MACHINE="sifive_u"
70    _PROJECT="qemu-sifive-u-test"
71    if (( $SMP == 1 )); then
72        SMP=2
73    fi
74    if (( $MEMSIZE == 512 )); then
75        MEMSIZE=8192
76    fi
77elif (( $DO_EMBEDDED == 1 )); then
78    QEMU="qemu-system-riscv32"
79    MACHINE="sifive_e"
80    _PROJECT="sifive-e-test"
81    MEMSIZE=0
82    SMP=0
83elif (( $DO_64BIT )); then
84    QEMU="qemu-system-riscv64"
85    CPU="rv64"
86    MACHINE="virt"
87    if (( $DO_SUPERVISOR )); then
88        _PROJECT="qemu-virt-riscv64-supervisor-test"
89        BIOS="default"
90    else
91        _PROJECT="qemu-virt-riscv64-test"
92        BIOS="none"
93    fi
94else
95    QEMU="qemu-system-riscv32"
96    CPU="rv32"
97    MACHINE="virt"
98    _PROJECT="qemu-virt-riscv32-test"
99    BIOS="none"
100fi
101if [[ -z "$PROJECT" ]]; then
102    PROJECT=$_PROJECT
103fi
104
105BLOCK_ARGS=" -drive if=none,file=blk.bin,id=blk,format=raw -device virtio-blk-device,drive=blk"
106NET_ARGS=" -netdev user,id=vmnic,hostname=qemu -device virtio-net-device,netdev=vmnic"
107NET_TAP_ARGS=" -netdev tap,id=vmnic -device virtio-net-device,netdev=vmnic"
108NO_DISPLAY_ARGS=" -nographic"
109DISPLAY_ARGS=" -device virtio-gpu-device -serial stdio"
110
111# construct a list of args based on previous variables
112ARGS=" -machine $MACHINE -kernel build-${PROJECT}/lk.elf"
113if [[ ! -z "$CPU" ]]; then
114    ARGS+=" -cpu $CPU"
115fi
116if (( $MEMSIZE )); then
117    ARGS+=" -m $MEMSIZE"
118fi
119if (( $SMP )); then
120    ARGS+=" -smp $SMP"
121fi
122if [[ ! -z "$BIOS" ]]; then
123    ARGS+=" -bios $BIOS"
124fi
125if (( $DO_BLOCK )); then
126    ARGS+=$BLOCK_ARGS
127fi
128if (( $DO_NET )); then
129    ARGS+=$NET_ARGS
130fi
131if (( $DO_NET_TAP )); then
132    ARGS+=$NET_TAP_ARGS
133    SUDO="sudo "
134fi
135if (( $DO_DISPLAY )); then
136    ARGS+=$DISPLAY_ARGS
137else
138    ARGS+=$NO_DISPLAY_ARGS
139fi
140
141MAKE_VARS=""
142
143if (( $DO_CMPCTMALLOC )); then
144    MAKE_VARS=LK_HEAP_IMPLEMENTATION=cmpctmalloc
145elif (( $DO_MINIHEAP )); then
146    MAKE_VARS=LK_HEAP_IMPLEMENTATION=miniheap
147fi
148
149$DIR/make-parallel $MAKE_VARS $PROJECT &&
150echo $SUDO $QEMU $ARGS $@ &&
151$SUDO $QEMU $ARGS $@
152