一、下载qemuqemu是一个模拟器可以模拟运行x86、arm、ppc、mips、riscv等CPU可以添加自定义开发板和硬件设备如pcie设备与vmware不同的是vmware是一个虚拟运行环境一个是模拟器一个是虚拟运行环境。下载qemu使用如下命令git clone https://gitlab.com/qemu-project/qemu.git二、编译qemu# cd qemu# ./configure --target-listx86_64-softmmu # 配置只编译x86系统模拟# make -j$(nproc) # 开始编译-j参数加快速度三、添加开发板如下是在qemu种添加一个AST2600ARM Cortex-A7核芯片的pf12开发板示例1、在hw/arm/aspeed.c下添加开发版描述// 在 hw/arm/aspeed.c 文件中 static const TypeInfo aspeed_machine_types[] { // ... 已有的 Machine 定义如 ast2600-evb { .name MACHINE_TYPE_NAME(pf12), // 命令行中使用的机器名 .parent TYPE_ASPEED_MACHINE, // 继承自 AST SoC 的 Machine 父类 .class_init aspeed_machine_pf12_class_init, // 初始化函数 }, // ... };2、实现class_init函数该函数需实现如下内容指定 CPU 型号设置开发板使用的 CPU 核心类型;定义内存布局配置 RAM 的起始地址和大小;添加外设通过函数调用将 UART、I2C、GPIO、MMC、网卡等外设挂载到总线上并分配它们的内存地址和中断号;配置启动流程指定固件如 U-Boot的加载地址和启动方式。四、添加硬件1、在hw/misc/或hw/char/创建test-device.c文件1、定义设备状态结构体 // 假设我们的设备叫 amd.xor-test typedef struct XorTestState { SysBusDevice parent_obj; // 父对象必须放在第一位 MemoryRegion iomem; // 设备的MMIO内存区域 qemu_irq irq; // 中断引脚 uint32_t regs[2]; // 两个32位寄存器 } XorTestState; 2、实现寄存器的读写逻辑 static const MemoryRegionOps xor_test_ops { .read xor_test_read, // 读操作函数 .write xor_test_write, // 写操作函数 .endianness DEVICE_LITTLE_ENDIAN, // ... }; 3、实现设备初始化和复位函数 static const TypeInfo xor_test_info { .name TYPE_XOR_TEST, // 设备类型名称如 amd.xor-test .parent TYPE_SYS_BUS_DEVICE, // 父类通常为系统总线设备 .instance_size sizeof(XorTestState), .class_init xor_test_class_init, .instance_init xor_test_init, }; 4、注册设备类型 static void xor_test_register_types(void) { type_register_static(xor_test_info); } type_init(xor_test_register_types) // 注册宏2、在hw/misc/Makefile.objs或hw/char/Makefile.objs添加如下obj-$(CONFIG_MY_DEVICE) test-device.o3、启动 QEMU 时-device 设备名 # 添加设别-device 设备名,help # 查看设备支持的属性-device 设备名,参数1val1,参数2val2,参数3val3 # 向设备传参五、启动qemugdb --args \ # 在 GDB 中启动 QEMU用于调试 QEMU 进程本身 qemu-system-x86_64 \ # 启动 x86_64 架构的系统模拟器 --enable-kvm \ # 启用 Linux 内核 KVM 加速利用宿主 CPU 硬件虚拟化提升性能 -boot c \ # 从硬盘disk C引导系统对应 -drive 中的第一个磁盘 -drive formatqcow2,file$UBUNTU/ubuntu-22.04-amd64.img \ # 定义系统盘格式为 qcow2支持快照、压缩 -drive formatraw,file$UBUNTU/disk_shared.ext4 \ # 定义第二块硬盘格式为裸设备 raw通常用作数据盘 -m 8G \ # 分配 8GB 客户机内存 -smp 4 \ # 为客户机配置 4 个 CPU 核心 -nic user,hostfwdtcp::$12345-:22 \ # 用户态网络堆栈将宿主机 12345 端口转发到虚拟机 22 端口SSH -nographic \ # 不使用图形界面将串口/控制台重定向到当前终端 -device edu,edu_id0,bar_size1G,offset0x10000/0x20000 # 启动edu模块这是一个教学用的开源pcie模块edu_id/bar_size/offset为传入模块的参数 -device test_device,device_id0,bar_size1G,offset0x10000/0x20000 # 启动test_device模块这是个自定义模块device_id/bar_size/offset为传入模块的参数六、常用命令查看支持的cpu型号qemu-system-x86_64 -cpu helpqemu-system-arm -cpu helpqemu-system-aarch64 -cpu helpqemu-system-ppc -cpu helpqemu-system-ppc64 -cpu helpqemu-system-mips -cpu helpqemu-system-riscv32 -cpu helpqemu-system-riscv64 -cpu help查看支持的开发板qemu-system-x86_64 -M helpqemu-system-arm -M helpqemu-system-aarch64 -M helpqemu-system-ppc -M helpqemu-system-ppc64 -M helpqemu-system-mips -M helpqemu-system-riscv32 -M helpqemu-system-riscv64 -M help