安装qemu
sudo apt install qemu-system-arm
安装编译调试工具
sudo apt install gcc-aarch64-linux-gnu
sudo apt install libncurses5-dev
sudo apt install gdb-multiarch
下载&编译Busybox
# busybox官网
https://busybox.net/
# Settings --->
# [*] Build static binary (no shared libs)
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j8
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- install
下载&编译kernel
# 下载网址
http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/
# General setup --->
# [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
# Device Drivers --->
# [*] Block devices --->
# <*> RAM block device support
# File systems --->
# <*> The Extended 4 (ext4) filesystem
make CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64 menuconfig
make CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64 -j8 Image
make CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64 defconfig
Image在目录“arch/arm64/boot”
制作rootfs
cd busybox
mkdir _install/proc _install/sys _install/dev _install/etc _install/etc/init.d
cat > _install/etc/init.d/rcS << EOF
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s
[ ! -h /etc/mtab ] && ln -s /proc/mounts /etc/mtab
[ ! -f /etc/resolv.conf ] && cat /proc/net/pnp > /etc/resolv.conf
EOF
chmod +x _install/etc/init.d/rcS
mkdir _rootfs
rsync -a busybox/_install/ _rootfs
sudo chown -R root:root _rootfs
cd _rootfs
find . | cpio -o --format=newc > ../rootfs.cpio
cd ..
gzip -c rootfs.cpio > rootfs.cpio.gz
执行qemu
qemu-system-aarch64 \
-nographic \
-no-reboot \
-machine virt \
-cpu cortex-a57 \
-smp 2 -m 256 \
-kernel Image \
-initrd rootfs.cpio.gz \
-append "panic=5 ro ip=dhcp root=/dev/ram rdinit=/sbin/init loglevel=8"
# 开启调试
qemu-system-aarch64 \
-nographic \
-no-reboot \
-machine virt \
-cpu cortex-a57 \
-smp 2 -m 256 \
-kernel Image \
-initrd rootfs.cpio.gz \
-append "panic=5 ro ip=dhcp root=/dev/ram rdinit=/sbin/init loglevel=8" \
-S -gdb tcp::9000
-nographic do not use graphics, and redirect serial I/O to console
-no-reboot exit instead of rebooting
-smp 核数目
-m 物理内存大小
-kernel 内核压缩镜像位置
-initrd rootfs位置
-append cmdline启动参数
-S 在入口处阻塞CPU
-gdb tcp::xxxx 指定通信通道为本地tcp,端口号为xxxx,如果不需要指定端口号可以用-s 代替
# 新开一个窗口启动gdb并链接
gdb-multiarch --tui vmlinux
(gdb)target remote :9000
(gdb)break start_kernel
(gdb)continue
(gdb)step
先按ctrl + a,再按x,可退出qemu