-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathrun
executable file
·270 lines (268 loc) · 6.87 KB
/
run
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bash
set -eu
. common
set -- ${cli_run:-} "$@"
# CLI handling.
cpus=1
debug_vm=
debug=false
kgdb=false
kvm=false
# norandmaps: Don't use address space randomization. Equivalent to echo 0 > /proc/sys/kernel/randomize_va_space.
# printk.time=y: log in format: "[time ] msg" for all printk messages.
# nokaslr: https://unix.stackexchange.com/questions/397939/turning-off-kaslr-to-debug-linux-kernel-using-qemu-and-gdb
# Turned on by default since v4.12
extra_append='nokaslr norandmaps printk.devkmsg=on printk.time=y'
extra_append_after_dash=
extra_flags=
extra_flags_qemu=
gem5=false
gem5opts=
lkmc_eval=
initrd=false
initramfs=false
memory=256M
nographic=true
root=
# A dummy value that is already turned on by default and does not produce large output,
# just to prevent QEMU from emitting a warning that '' is not valid.
trace_enable=pr_manager_run
while getopts a:c:DdE:e:f:G:ghIiKkm:T:x OPT; do
case "$OPT" in
a)
arch="$OPTARG"
;;
c)
cpus="$OPTARG"
;;
D)
debug_vm='gdb -q -ex start --args'
;;
d)
debug=true
extra_flags_qemu="$extra_flags_qemu -S"
;;
E)
lkmc_eval="$OPTARG"
;;
e)
extra_append="$extra_append $OPTARG"
;;
f)
extra_append_after_dash="$extra_append_after_dash $OPTARG"
;;
G)
gem5opts="$OPTARG"
;;
g)
gem5=true
;;
h)
cat build-usage.adoc 1>&2
exit
;;
I)
initramfs=true
;;
i)
initrd=true
;;
K)
kvm=true
;;
k)
extra_append="$extra_append kgdbwait"
# For those who want to try KDB.
#extra_append="$extra_append kgdbwait kgdboc=kbd"
extra_flags_qemu="$extra_flags_qemu -serial tcp::1234,server,nowait"
kgdb=true
;;
m)
memory="$OPTARG"
;;
T)
trace_enable="$OPTARG"
;;
x)
nographic=false
;;
?)
exit 2
;;
esac
done
shift "$(($OPTIND - 1))"
extra_flags="$extra_flags $@"
set_common_vars "$arch" "$gem5"
images_dir="${buildroot_out_dir}/images"
if "$debug" && "$kvm"; then
echo 'error: -d and -K are incompatible' 1>&2
exit 1
fi
if "$initrd" || "$initramfs"; then
ramfs=true
else
ramfs=false
fi
if [ -n "$lkmc_eval" ]; then
if "$ramfs"; then
initarg="rdinit"
else
initarg="init"
fi
extra_append="${extra_append} ${initarg}=/eval_base64.sh"
extra_append_after_dash="${extra_append_after_dash} lkmc_eval=\"$(printf "$lkmc_eval" | base64)\""
fi
if "$nographic" && [ "$arch" = x86_64 ]; then
extra_append="$extra_append console=ttyS0"
extra_flags_qemu="$extra_flags_qemu -nographic"
fi
if [ -n "$extra_append_after_dash" ]; then
extra_append="${extra_append} - ${extra_append_after_dash}"
fi
if "$gem5"; then
gem5_build_dir="${buildroot_out_dir}/build/gem5-1.0"
gem5_src_dir="${gem5_build_dir}/gem5"
memory="${memory}B"
if [ "$arch" = x86_64 ]; then
gem5_arch=X86
else
gem5_arch=ARM
fi
gem5_common="\
M5_PATH='${gem5_build_dir}/system' \
${debug_vm} \
'${gem5_src_dir}/build/${gem5_arch}/gem5.opt' \
--debug-file=trace.txt \
${gem5opts} \
-d '${m5out_dir}' \
'${gem5_src_dir}/configs/example/fs.py' \
--disk-image='${images_dir}/rootfs.ext2' \
--kernel='${buildroot_out_dir}/build/linux-custom/vmlinux' \
--mem-size=${memory} \
--num-cpus='${cpus}' \
--script='${root_dir}/readfile.gitignore' \
"
if [ "$arch" = x86_64 ]; then
if "$kvm"; then
extra_flags="$extra_flags --cpu-type=X86KvmCPU"
fi
cmd="\
${gem5_common} \
--command-line='earlyprintk=ttyS0 console=ttyS0 lpj=7999923 root=/dev/hda ${extra_append}' \
${extra_flags} \
"
elif [ "$arch" = arm ] || [ "$arch" = aarch64 ]; then
# TODO why is it mandatory to pass mem= here? Not true for QEMU.
# Anything smaller than physical blows up as expected, but why can't it auto-detect the right value?
cmd="${gem5_common} \
--command-line='earlyprintk=pl011,0x1c090000 console=ttyAMA0 lpj=19988480 rw loglevel=8 mem=${memory} root=/dev/sda ${extra_append}' \
--dtb-file='${gem5_src_dir}/system/arm/dt/$([ "$arch" = arm ] && echo "armv7_gem5_v1_${cpus}cpu" || echo "armv8_gem5_v1_${cpus}cpu").dtb' \
--machine-type=VExpress_GEM5_V1 \
${extra_flags} \
"
fi
else
if "$kvm"; then
extra_flags="${extra_flags} -enable-kvm"
fi
extra_flags="${extra_flags_qemu} ${extra_flags}"
qemu_common="\
${debug_vm} \
'${buildroot_out_dir}/host/usr/bin/qemu-system-${arch}' \
-gdb tcp::1234 \
-m ${memory} \
-monitor telnet::45454,server,nowait \
-netdev user,hostfwd=tcp::45455-:45455,hostfwd=tcp::45456-:22,id=net0 \
-smp ${cpus} \
-trace enable=${trace_enable},file='${qemu_out_dir}/trace.bin' \
-virtfs local,path=9p,mount_tag=host_scratch,security_model=mapped,id=host_scratch \
-virtfs local,path=${buildroot_out_dir}/build,mount_tag=host_out,security_model=mapped,id=host_out \
"
if "$initrd"; then
extra_flags="${extra_flags} -initrd '${images_dir}/rootfs.cpio'"
fi
if "$ramfs"; then
# TODO why is this needed, and why any string works.
root='root=/dev/anything'
fi
# The base QEMU commands are found under board/qemu/*/readme.tx
case "$arch" in
x86_64)
if "$kgdb"; then
extra_append="${extra_append} kgdboc=ttyS0,115200"
fi
if ! "$ramfs"; then
root='root=/dev/vda'
extra_flags="${extra_flags} -drive file='${images_dir}/rootfs.ext2.qcow2,if=virtio,format=qcow2'"
fi
cmd="\
${qemu_common} \
-M pc \
-append '${root} nopat ${extra_append}' \
-device edu \
-device lkmc_pci_min \
-device virtio-net-pci,netdev=net0 \
-kernel '${images_dir}/bzImage' \
${extra_flags} \
"
;;
arm)
if "$kgdb"; then
extra_append="${extra_append} kgdboc=ttyAMA0,115200"
fi
if ! "$ramfs"; then
extra_flags="${extra_flags} -drive file='${images_dir}/rootfs.ext2.qcow2,if=scsi,format=qcow2'"
root='root=/dev/sda'
fi
cmd="\
$qemu_common \
-M versatilepb \
-append '${root} ${extra_append}' \
-device rtl8139,netdev=net0 \
-dtb '${images_dir}/versatile-pb.dtb' \
-kernel '${images_dir}/zImage' \
-serial stdio \
$extra_flags \
"
;;
aarch64)
if "$kgdb"; then
extra_append="${extra_append} kgdboc=ttyAMA0,115200"
fi
if ! "$ramfs"; then
root='root=/dev/vda'
extra_flags="${extra_flags} -drive file='${images_dir}/rootfs.ext2.qcow2,if=virtio,format=qcow2'"
fi
cmd="\
${qemu_common} \
-M virt \
-append '${root} ${extra_append}' \
-cpu cortex-a57 \
-device virtio-net-device,netdev=net0 \
-kernel '${images_dir}/Image' \
-nographic \
-serial stdio \
${extra_flags} \
"
;;
mips64)
if ! "$ramfs"; then
root='root=/dev/hda'
extra_flags="${extra_flags} -drive file='${images_dir}/rootfs.ext2.qcow2,format=qcow2'"
fi
cmd="\
${qemu_common} \
-M malta \
-append '${root} ${extra_append}' \
-cpu I6400 \
-device pcnet \
-kernel '${images_dir}/vmlinux' \
-nographic \
${extra_flags} \
"
;;
esac
fi
echo "$cmd" | tee run.log
eval "$cmd"