Files
linux-kernel-module-cheat/run
Ciro Santilli a5b35bef11 Enable either ext2, initrd or initramfs for x86, arm and aarch64
Mention that initrd and initramfs must fit into memory.

Fix missing stdout when ./run -d is used.

Ignore ./run -n for non x86.
2018-03-16 16:34:57 +00:00

248 lines
6.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
# CLI handling.
arch=x86_64
cpus=1
debug_vm=''
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_flags=''
extra_flags_qemu=''
gem5=false
gem5opts=''
lkmc_eval=''
initrd=false
initramfs=false
memory=128M
nographic=false
root=''
while getopts a:c:DdE:e:G:gIiKkm:nt:x OPT; do
case "$OPT" in
a)
arch="$OPTARG"
;;
c)
cpus="$OPTARG"
;;
d)
extra_flags_qemu="$extra_flags_qemu -S -s"
;;
D)
debug_vm='gdb -q -ex start --args'
;;
E)
lkmc_eval="$OPTARG"
;;
e)
extra_append="$extra_append $OPTARG"
;;
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
;;
g)
gem5=true
;;
G)
gem5opts="$OPTARG"
;;
I)
initramfs=true
;;
i)
initrd=true
;;
m)
memory="$OPTARG"
;;
n)
nographic=true
;;
esac
done
shift "$(($OPTIND - 1))"
extra_flags="$extra_flags $@"
arch_dir="$arch"
if "$gem5"; then
arch_dir="${arch}-gem5"
fi
root_dir="$(pwd)"
buildroot_dir="${root_dir}/buildroot"
out_dir="${root_dir}/buildroot/output.${arch_dir}~"
images_dir="${out_dir}/images"
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 - 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 "$gem5"; then
build_dir="${out_dir}/build/gem5-1.0"
memory="${memory}B"
gem5_dir="${build_dir}/gem5"
if [ "$arch" = x86_64 ]; then
gem5_arch=X86
else
gem5_arch=ARM
fi
gem5_cpt_dir="./m5out/cpts/${arch}"
mkdir -p "$gem5_cpt_dir"
gem5_common="\
M5_PATH='${build_dir}/system' \
$debug_vm \
'${gem5_dir}/build/${gem5_arch}/gem5.opt' \
${gem5opts} \
'${gem5_dir}/configs/example/fs.py' \
--checkpoint-dir='${gem5_cpt_dir}' \
--disk-image='${out_dir}/images/rootfs.ext2' \
--kernel='${out_dir}/build/linux-custom/vmlinux' \
--mem-size=${memory} \
--num-cpus='${cpus}' \
"
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_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}"
images_dir="${out_dir}/images"
qemu_common="\
${debug_vm} \
'${out_dir}/host/usr/bin/qemu-system-${arch}' \
-m ${memory} \
-monitor telnet::45454,server,nowait \
-netdev user,hostfwd=tcp::45455-:45455,id=net0 \
-smp $cpus \
-virtfs local,path=9p,mount_tag=host_scratch,security_model=mapped,id=host_scratch \
-virtfs local,path=${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
# 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/anything'
else
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
root='root=/dev/anything'
else
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/anything'
else
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/anything'
else
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"