QEMU使我们在没有外设设备下常用的调试Linux软件,可以用来模拟系统启动;
并支持GDB,在没有外设的情况,用来学习和跟踪代码的最好工具之一。

我们在学习一个架构前,往往需要跟踪代码流程,那么qemu就是最适合的工具。

依赖环境

  1. sudo apt-get install -y qemu gcc make gdb git figlet
  2. sudo apt-get install -y libncurses5-dev iasl wget
  3. sudo apt-get install -y device-tree-compiler
  4. sudo apt-get install -y flex bison libssl-dev libglib2.0-dev
  5. sudo apt-get install -y libfdt-dev libpixman-1-dev
  6. sudo apt-get install -y python pkg-config u-boot-tools intltool xsltproc
  7. sudo apt-get install -y gperf libglib2.0-dev libgirepository1.0-dev
  8. sudo apt-get install -y gobject-introspection
  9. sudo apt-get install -y python2.7-dev python-dev bridge-utils
  10. sudo apt-get install -y uml-utilities net-tools
  11. sudo apt-get install -y libattr1-dev libcap-dev
  12. sudo apt-get install -y kpartx libsdl2-dev libsdl1.2-dev
  13. sudo apt-get install -y debootstrap bsdtar
  14. sudo apt-get install -y libelf-dev gcc-multilib g++-multilib
  15. sudo apt-get install -y libcap-ng-dev libaio-dev
  16. sudo apt-get install -y libcap-dev libattr1-dev figlet libssl-dev

相关代码下载

快速部署

可以通过以下链接快速部署:
https://github.com/vici-by/Linux-kernel-test/blob/main/kernel-test/vidi-env-init.sh

相关下载

Linux内核下载 https://mirror.bjtu.edu.cn/kernel/linux/kernel/
UBOOT下载 ftp://ftp.denx.de/pub/u-boot
busybox下载 https://busybox.net/downloads/

QEMU模拟X86_64启动

QEMU模拟X86_64 linux

package.rar :网络配置文件
RunBiscuitOS.sh :qemu测试demo脚本
参考文档:用Qemu搭建x86学习环境

下载相关文件

  1. mkdir x86_64 -p && cd x86_64
  2. x86_64_dir=$(pwd)
  3. export LINUX="linux-5.8.14"
  4. export QEMU="qemu-5.1.0"
  5. export BUSYBOX="busybox-1.32.0"
  6. export EDK2="edk2-UDK2018"
  7. wget https://mirror.bjtu.edu.cn/kernel/linux/kernel/v5.x/${LINUX}.tar.gz && \
  8. tar -zxf ${LINUX}.tar.gz && rm ${LINUX}.tar.gz
  9. wget https://download.qemu.org/${QEMU}.tar.xz && \
  10. tar xJf ${QEMU}.tar.xz && rm ${QEMU}.tar.xz
  11. wget https://busybox.net/downloads/${BUSYBOX}.tar.bz2 && \
  12. tar jxf ${BUSYBOX}.tar.bz2 && rm ${BUSYBOX}.tar.bz2
  13. wget https://github.com/tianocore/edk2/archive/UDK2018.zip && \
  14. unzip edk2-${EDK2}.zip && rm ${EDK2}.zip
  15. ln -s ${x86_64_dir}/${LINUX} ${x86_64_dir}/linux
  16. ln -s ${x86_64_dir}/${QEMU} ${x86_64_dir}/qemu
  17. ln -s ${x86_64_dir}/${BUSYBOX} ${x86_64_dir}/busybox
  18. ln -s ${x86_64_dir}/${EDK2} ${x86_64_dir}/edk2
  19. # 将package.rar放到当前目录,解压
  20. sudo ./package/networking/bridge.sh
  21. sudo cp ./package/networking/qemu-* /etc/

编译并支持qemu-system-x86_64

  1. cd qemu
  2. ./configure --prefix=/opt/x86_64/qemu-x86-bin --target-list=x86_64-softmmu --gdb=/usr/bin/gdb \
  3. --enable-linux-aio --enable-debug --enable-debug-info
  4. sudo make && sudo make install
  5. sudo echo "PATH=\$PATH:/usr/local/qemu_x86/bin" >> ~/.bashrc
  6. source ~/.bashrc
  7. which qemu-system-x86_64

编译内核

  1. cd linux
  2. make x86_64_defconfig
  3. make menuconfig
  4. // 这部分默认内核都有配置,这里强调下
  5. General setup --->
  6. [*]Initial RAM filesystem and RAM disk (initramfs/initrd) support
  7. Device Driver --->
  8. [*] Block devices --->
  9. <*> RAM block device support
  10. (153600) Default RAM disk size
  11. make -j$(cat /proc/cpuinfo | grep processor | wc -l)

编译打包文件系统

可能需要下载相应版本编译器:http://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz 否则C程序运行不了

  1. cd busybox
  2. make menuconfig
  3. Settings --->
  4. --- Build Options
  5. [*] Build static binary (no shared libs)
  6. rm _install ; make -j$(cat /proc/cpuinfo | grep processor | wc -l) && make install
  7. cd ../
  8. # need root
  9. BUSYBOX=$(realpath busybox)
  10. OUTPUT=$(realpath ./)
  11. ROOTFS_NAME=x86_64
  12. ROOTFS_SIZE=200
  13. FS_TYPE=ext4
  14. sudo rm ${OUTPUT}/rootfs/ -rf
  15. sudo rm x86_64.img
  16. mkdir -p ${OUTPUT}/rootfs
  17. sudo cp ${BUSYBOX}/_install/* ${OUTPUT}/rootfs -raf
  18. sudo chown root.root ${OUTPUT}/rootfs/* -R
  19. sudo cp ${BUSYBOX}/examples/bootfloppy/etc ${OUTPUT}/rootfs -arf
  20. sudo sed -r "/askfirst/ s/.*/::respawn:-\/bin\/sh/" ${OUTPUT}/rootfs/etc/inittab -i
  21. sudo mkdir -p ${OUTPUT}/rootfs/lib
  22. sudo mkdir -p ${OUTPUT}/rootfs/proc
  23. sudo mkdir -p ${OUTPUT}/rootfs/sys
  24. sudo mkdir -p ${OUTPUT}/rootfs/tmp
  25. sudo mkdir -p ${OUTPUT}/rootfs/root
  26. sudo mkdir -p ${OUTPUT}/rootfs/var
  27. sudo mkdir -p ${OUTPUT}/rootfs/mnt
  28. sudo mkdir -p ${OUTPUT}/rootfs/dev
  29. sudo mknod ${OUTPUT}/rootfs/dev/tty1 c 4 1
  30. sudo mknod ${OUTPUT}/rootfs/dev/tty2 c 4 2
  31. sudo mknod ${OUTPUT}/rootfs/dev/tty3 c 4 3
  32. sudo mknod ${OUTPUT}/rootfs/dev/tty4 c 4 4
  33. sudo mknod ${OUTPUT}/rootfs/dev/console c 5 1
  34. sudo mknod ${OUTPUT}/rootfs/dev/null c 1 3
  35. sudo echo "mount -t sysfs sysfs /sys" >> ${OUTPUT}/rootfs/etc/init.d/rcS
  36. sudo echo "mount -t tracefs nodev /sys/kernel/tracing" >> ${OUTPUT}/rootfs/etc/init.d/rcS
  37. sudo echo "mount -t debugfs nodev /sys/kernel/debug" >> ${OUTPUT}/rootfs/etc/init.d/rcS
  38. dd if=/dev/zero of=${OUTPUT}/ramdisk bs=1M count=${ROOTFS_SIZE}
  39. mkfs.ext4 -E lazy_itable_init=1,lazy_journal_init=1 -F ${OUTPUT}/ramdisk
  40. mkdir -p ${OUTPUT}/tmpfs
  41. sudo mount -t ${FS_TYPE} ${OUTPUT}/ramdisk ${OUTPUT}/tmpfs/ -o loop
  42. sudo cp -raf ${OUTPUT}/rootfs/* ${OUTPUT}/tmpfs/
  43. sudo umount ${OUTPUT}/tmpfs
  44. mv ${OUTPUT}/ramdisk ${OUTPUT}/${ROOTFS_NAME}.img
  45. sudo rm ${OUTPUT}/rootfs/ ${OUTPUT}/tmpfs -rf
  46. # 挂载外接盘
  47. dd if=/dev/zero of=Freeze.img bs=1M count=128
  48. sudo mkfs.ext4 -F ./Freeze.img
  49. mkdir -p freezeDir && FREEDIR=$(realpath freezeDir)
  50. sudo mount -t ext4 -o loop ./Freeze.img ${FREEDIR}
  51. # .... 拷贝测试文件
  52. sync && sudo umount ${FREEDIR}

qemu 模拟启动

  1. 注:这部分需要主机开启VM虚拟化
  2. kvm-ok命令检测
  3. baiy@ubuntu:x86_64$ qemu-system-x86_64 -cpu help
  4. x86 base base CPU model type with no features enabled
  5. x86 host KVM processor with all supported host features
  6. x86 max Enables all features supported by the accelerator in the current host
  7. # common usual
  8. /usr/local/qemu_x86/bin/qemu-system-x86_64 \
  9. -smp 2 \
  10. -cpu host \
  11. -enable-kvm \
  12. -m 512M \
  13. -kernel linux/arch/x86/boot/bzImage \
  14. -hda ./x86_64.img \
  15. -hdb ./Freeze.img \
  16. -nographic \
  17. -append "root=/dev/sda rw rootfstype=ext4 console=ttyS0 init=linuxrc loglevel=8"
  18. # support network
  19. /usr/local/qemu_x86/bin/qemu-system-x86_64 \
  20. -smp 2 \
  21. -cpu host \
  22. -enable-kvm \
  23. -m 512M \
  24. -kernel linux/arch/x86/boot/bzImage \
  25. -hda ./x86_64.img \
  26. -hdb ./Freeze.img \
  27. -nographic \
  28. -append "root=/dev/sda rw rootfstype=ext4 console=ttyS0 init=linuxrc loglevel=8" \
  29. -net tap \
  30. -device virtio-net-device,netdev=bsnet0,mac=E0:FE:D0:3C:2E:EE \
  31. -netdev tap,id=bsnet0,ifname=bsTap0
  32. # debug
  33. /usr/local/qemu_x86/bin/qemu-system-x86_64 \
  34. -smp 2 \
  35. -cpu host \
  36. -enable-kvm \
  37. -m 512M \
  38. -kernel linux/arch/x86/boot/bzImage \
  39. -hda ./x86_64.img \
  40. -hdb ./Freeze.img \
  41. -nographic \
  42. -append "root=/dev/sda rw rootfstype=ext4 console=ttyS0 init=linuxrc loglevel=8" \
  43. -S -s
  44. gdb vmlinux
  45. target remote localhost:1234
  46. b start_kernel
  47. continue

QEMU模拟ARM64启动

环境搭建

  1. # 相关软件包下载
  2. [official git]
  3. git clone git://git.denx.de/u-boot.git
  4. git clone https://github.com/torvalds/linux
  5. git clone git://busybox.net/busybox.git
  6. [local download]
  7. ftp://ftp.denx.de/pub/u-boot/
  8. https://mirror.bjtu.edu.cn/kernel/linux/kernel/v4.x/
  9. https://busybox.net/downloads/
  10. 交叉编译器下载
  11. https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
  12. https://www.linaro.org/downloads/

QEMU模拟ATF+UBOOT

QEMU模拟Linux

QEMU模拟ATF+UBOOT+Linux全流程


QEMU模拟ARM32启动

QEMU模拟UBOOT

QEMU模拟Linux

QEMU模拟UBOOT+Linux全流程