系列文章 : simulation / emulation 學習筆記
在這一篇裡,我們希望可以在 gem5 上開機 ARM 的 linux kernel,並紀錄下每一個過程中使用的指令。
之前已經有開過 RISC-V 的了,可以參考這一篇 : [gem5][riscv][linux] 使用 gem5 來開啟 linux-kernel
使用 buildroot 幫我們產生出 root filesystem
之後也會借用 buildroot 的 toolchain 來編譯其他東西
git clone https://github.com/buildroot/buildroot.git
cd buildroot
# checkout 到比較穩定的 commit
git checkout 56c6862bc81ef41c0fe012677eafa24381b1f76c
# use default configuration for aarch 64-bit virt QEMU/gem5
make qemu_aarch64_virt_defconfig
make menuconfig
# Disable RPC support
# BR2_TOOLCHAIN_EXTERNAL_INET_RPC is not set
# Serial Redirect to ttyAMA0 for gem5 console
#BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
System configuration
→Run a getty (login prompt) after boot
→(console) TTY port
→ttyAMA0
# Disable redundant Linux Kernel compilation
# BR2_LINUX_KERNEL is not set
Kernel
→[ ] Linux Kernel
# ext4
Filesystem images
→ext2/3/4 root filesystem
→ext4
# Resolve dependencies and cleanup
make olddefconfig
# Build the isolated root filesystem
make -j$(nproc)
cd ../
借用 buildroot 的 toolchain 來編譯 linux kernel
# 或許可以嘗試看看更新的 linux kernel ( e.g. v6.8 )
git clone --depth 1 --branch v5.15 https://github.com/torvalds/linux.git
cd linux
# generate default RISC-V configuration
make ARCH=arm64 CROSS_COMPILE=$(realpath ../buildroot/output/host/bin)/aarch64-linux- defconfig
# Compile both the vmlinux ELF (needed for debugging) and the Image (used as payload)
make ARCH=arm64 CROSS_COMPILE=$(realpath ../buildroot/output/host/bin)/aarch64-linux- vmlinux Image -j48
cd ../
git clone --branch develop https://github.com/gem5/gem5.git
cd gem5
# Build the gem5 Full-System Simulator (ARM)
scons build/ARM/gem5.opt -j$(nproc)
# Build the serial terminal interface utility
make -C util/term
cd ..
編譯 gem5 裡面的,用來開 ARM 的 bootloader
cd gem5
make -C ./system/arm/bootloader/arm64 CROSS_COMPILE=$(realpath ../buildroot/output/host/bin)/aarch64-linux-
cd ../
# Create directory tree
mkdir -p gem5-resources/binaries gem5-resources/disks
# Copy Kernel ELF
cp linux/vmlinux gem5-resources/binaries/vmlinux
# Copy Bootloaders
cp gem5/system/arm/bootloader/arm64/boot.arm64 gem5-resources/binaries/boot.arm64
cp gem5-resources/binaries/boot.arm64 gem5-resources/binaries/boot.arm
# Copy Filesystem Disk Image
cp buildroot/output/images/rootfs.ext4 gem5-resources/disks/rootfs.ext4
cd gem5
# Set M5 PATH to our organized resource folder
export M5_PATH=$(realpath ../gem5-resources)
# Run the Simulator
# 也可以試試看 O3 CPU, minor CPU,或是更換不同的 `num-cores`
./build/ARM/gem5.opt configs/example/arm/starter_fs.py \
--kernel=vmlinux \
--disk-image=rootfs.ext4 \
--cpu="atomic" \
--num-cores=1 \
--root-device=/dev/vda
之後在另外一個 shell 用 gem5 的 terminal 連進虛擬機裡面。
./util/term/gem5term 3456
…
Welcome to Buildroot
buildroot login: root
#
成功的話,可以看到 login 的地方,這邊輸入 root 即可登入 shell。
O3 CPU model 開 linux kernel 會 panic … 可能要找時間來 debug,或是等待有志之士提供解法了。