在 x86 上使用 QEMU 虚拟机模拟并调试 ARM64。
参考:ubuntu虚拟机中使用QEMU搭建ARM64环境
sudo apt-get install gcc-aarch64-linux-gnu
命令安装以及源码安装均不支持树莓派4B平台
sudo apt install qemu-system-arm
,qemu-system-aarch64 --help
export ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
但是,命令安装不支持树莓派4。
jiaming@jiaming-pc:~/Documents/jailhouse-rpi4$ qemu-system-aarch64 -M ?
Supported machines are:
...
raspi2 Raspberry Pi 2
raspi3 Raspberry Pi 3
realview-eb ARM RealView Emulation Baseboard (ARM926EJ-S)
...
安装 ninja。
git clone https://github.com/ninja-build/ninja.git && cd ninja
./configure.py --bootstrap
cp ninja /usr/bin/
./configure --target-list=aarch64-softmmu,aarch64-linux-user --enable-debug
make -j4
make install
$ sudo apt-get install bison flex bc build-essential libncurses* libssl-dev
$ sudo apt-get install qemu-user-static binfmt-support debootstrap
$ sudo su - root
$ debootstrap --arch=arm64 --foreign buster linux_rootfs http://mirrors.ustc.edu.cn/debian/ # linux_rootfs:本地目录,最后制作好的文件系统会在此目录
$sudo cp /usr/bin/qemu-aarch64-static ./linux_rootfs/usr/bin/
$ sudo chroot linux_rootfs/ debootstrap/debootstrap --second-stage
$ chroot linux_rootfs/
配置root用户密码
passwd root
添加用户以及设置密码
useradd -G sudo -m -s /bin/bash XXX
passwd XXX
设置主机名
echo XXX-PC > /etc/hostname
设置网络
$ echo "auto lo" > /etc/network/interfaces
$ echo "iface lo inet loopback" >> /etc/network/interfaces
$ echo "allow-hotplug enp0s1" > /etc/network/interfaces
$ echo "iface enp0s1 inet dhcp" >> /etc/network/interfaces
安装依赖
$ apt update
$ apt install net-tools build-essential
$ dd if=/dev/zero of=linux_rootfs.ext4 bs=1M count=2048
使用 mkfs.ext4 命令格式化:
$ mkfs.ext4 linux_rootfs.ext4
挂载并拷贝内容:
$ mkdir -p tmpfs
$ sudo mount -t ext4 linux_rootfs.ext4 tmpfs/ -o loop
$ sudo cp -af linux_rootfs/* tmpfs/
$ sudo umount tmpfs
$ sudo chmod 777 linux_rootfs.ext4
$ cd linux-4.14.221/
$ export ARCH=arm64 ROSS_COMPILE=aarch64-linux-gnu-
$ make defconfig
$ make -j4
编译内核时开启 CONFIG_GDB_SCRIPTS
,关闭 CONFIG_DEBUG_INFO_REDUCED
。 如果架构支持 CONFIG_FRAME_POINTER
,请保持开启。
编译成功后,在 arch/arm64/boot/
目录下生成 Image 文件。
启动命令:
qemu-system-aarch64 \
-m 1024 \
-cpu cortex-a57 \
-M virt -nographic \
-smp 4 \
-kernel linux-4.14.221/arch/arm64/boot/Image \
-append "noinintrd sched_debug root=/dev/vda rootfstype=ext4 rw crashkernel=256M loglevel=8" \
-drive if=none,file=linux_rootfs.ext4,id=hd0 \
-device virtio-blk-device,drive=hd0
qemu-system-aarch64
: QEMU 指令,用于启动一个 AARCH64 虚拟机。-m 1024
: 设置虚拟机的内存大小为 1GB。-cpu cortex-a57
: 执行虚拟机使用的CPU型号 A57。-M virt -nographic
: 设置 QEMU 为虚拟化模式,并且不显示图形界面。-smp 4
: 设置虚拟机支持的最大线程数目为 4。-kernel linux-4.14.221/arch/arm64/boot/Image
: 指定要加载的 Linux 内核文件。-append "noinintrd sched_debug root=/dev/vda rootfstype=ext4 rw crashkernel=256M loglevel=8"
: 添加内核参数,以启用 noinintrd sched_debug 调试选项,并将根目录设置为 VDA 中的 /dev/vda。if=none,file=linux_rootfs.ext4,id=hd0
: 指定要使用的存储设备为 IDE 控制器上的 HD0 设备,文件名为 linux_rootfs.ext4。-device virtio-blk-device,drive=hd0
: 指定要使用的 virtio blk 设备,用于存储设备。生成编译引导文件
./bootstrap
arm64交叉编译
./configure CC=aarch64-linux-gnu-gcc LD=aarch64-linux-gnu-ld --host=aarch64-linux --enable-mpers=no
编译
make LDFLAGS+="-static -pthread"
添加至根文件系统
sudo mount -t ext4 linux_rootfs.ext4 tmpfs/ -o loop
sudo cp -af strace/src/strace tmpfs/bin/
sudo umount tmpfs
不支持 gui。
sudo apt-get install g++-9-aarch64-linux-gnu
export CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++-9
cd gdb-8.0/
mkdir gdb-build
./configure --host=aarch64-linux-gnu --target=aarch64-linux-gnu --program-prefix=aarch64-linux- --prefix=/home/jiaming/Documents/jailhouse-rpi4/qemu-arm64/gdb-8.0/gdb-build
make
sudo make install
sudo cp /usr/aarch64-linux-gnu/lib/* /lib/
出现编译错误以及解决方案:https://blog.csdn.net/weixin_44602409/article/details/115716913
vim ~/.bashrc
,添加:
export PATH=$PATH:/home/jiaming/Documents/jailhouse-rpi4/qemu-arm64/gdb-8.0/gdb-build/bin
-S -s
参数。内核启动挂起,等待 gdb 连接。
jiaming@jiaming-pc:~/Documents/jailhouse-rpi4/qemu-arm64/linux-4.14.221$ aarch64-linux-gdb vmlinux
,并使用默认端口连接,target remote localhost:1234
。make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- LD=/home/jiaming/gnu/aarch64/lin/aarch64-linux/bin/aarch64-linux-gnu-ld
make ARCH=arm64 CROSS_COMPILE=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- CC=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc LD=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ld AR=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ar install
wget https://busybox.net/downloads/busybox-1.29.0.tar.bz2
tar -xjf busybox-1.29.0.tar.bz2
cd busybox-1.29.0
make ARCH=arm64 CROSS_COMPILE=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- CC=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc LD=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ld AR=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ar menuconfig
make ARCH=arm64 CROSS_COMPILE=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- CC=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc LD=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ld AR=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ar
make ARCH=arm64 CROSS_COMPILE=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- CC=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc LD=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ld AR=/home/jiaming/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ar install
dd if=/dev/zero of=busybox-1.29.0-rootfs_ext4.img bs=1M count=100 oflag=direct
mkfs.ext4 busybox-1.29.0-rootfs_ext4.img
mkdir rootfs
sudo mount busybox-1.29.0-rootfs_ext4.img rootfs/
sudo cp -raf busybox-1.29.0/_install/* rootfs/
cd rootfs
sudo mkdir -p proc sys tmp root var mnt dev
sudo mknod dev/tty1 c 4 1
sudo mknod dev/tty2 c 4 2
sudo mknod dev/tty3 c 4 3
sudo mknod dev/tty4 c 4 4
sudo mknod dev/console c 5 1
sudo mknod dev/null c 1 3
sudo cp -r ../busybox-1.29.0/examples/bootfloppy/etc/ .
cd ..
sudo umount rootfs
QEMU 启动命令:qemu-system-aarch64 -m 1024 -cpu cortex-a57 -machine virt,gic-version=3,virtualization=on -kernel Image -append "console=ttyAMA0 root=/dev/vda init=/linuxrc rw" -hda busybox-1.29.0-rootfs_ext4.img -monitor stdio
挂载 /dev/vda 作为根文件系统,需要在内核中配置如下选项
CONFIG_VIRTIO_BLK=y
CONFIG_VIRTIO_PCI=y
CONFIG_EXT4_FS=y
否则会出现该错误:
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(254,0)
安装 sdl
安装 sdl: https://blog.csdn.net/qq_22948593/article/details/109740372,不需要交叉编译。
https://www.libsdl.org/release/SDL2-2.0.14.tar.gz
重新安装 QEMU
./configure --target-list=aarch64-softmmu,aarch64-linux-user --enable-debug --enable-sdl
make -j4
make install
有 -nographic
参数:
无 -nographic
参数: