run 6.9 KB

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