M1 Mac双系统搭建qemu riscv linux仿真

前言

实验需要riscv仿真,网上没有针对m1的配置教程,故在此整理下。本人用的m1macbookpro,系统12.3。

参考到的链接:
https://github.com/AsahiLinux
https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html
https://zhuanlan.zhihu.com/p/258394849
https://zhuanlan.zhihu.com/p/555398048

第一部分 m1双系统

安装asashi linux仅需一行命令,安装按照提示执行即可:

curl https://alx.sh | sh

第二部分 编译riscv工具链

安装依赖

$ sudo pacman -Syyu autoconf automake curl python3 libmpc mpfr gmp gawk base-devel bison flex texinfo gperf libtool patchutils bc zlib expat

下载工具链源码

$ git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
$ cd riscv-gnu-toolchain
$ ./configure --prefix=/opt/riscv
$ make linux -j$(nproc)

之后在.zshrc中添加

export PATH="$PATH:/opt/riscv/bin"
export RISCV="/opt/riscv"

测试 toolchain 是否安装成功

$ riscv64-unknown-linux-gnu-gcc -v

第三部分 编译qemu和linux内核

下载源码

$ mkdir riscv64-linux
$ cd riscv64-linux
$ git clone https://github.com/qemu/qemu
$ git clone https://github.com/torvalds/linux
$ git clone https://git.busybox.net/busybox

编译qemu

$ cd qemu
$ git checkout v5.1.0
$ ./configure --target-list=riscv64-softmmu
$ make -j $(nproc)
$ sudo make install

这里会遇到一些类型转换和数组对齐报错,通过在报错的文件顶端添加#pragma GCC diagnostic ignored “-Wxx”可忽略这些错误,他们是编译器版本不同导致的,xx换成报的error即可。

编译linux

$ cd linux
$ git checkout v5.4.0
$ make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- defconfig
$ make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j $(nproc)

这里会遇到一个重定义报错,将其中一个作为实际变量,其他出现的地方添加extern可解决。

编译busybox

$ cd busybox
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make menuconfig

打开配置菜单后进入第一行的 “Settings”,在"Build Options"节中,选中 “Build static binary (no shared libs)”,设置好后退出保存配置

$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make defconfig
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make -j $(nproc)
$ CROSS_COMPILE=riscv64-unknown-linux-gnu- make install

编译完后在目录下会有一个_install目录。

启动仿真

创建文件系统

$ cd riscv64-linux
$ qemu-img create rootfs.img  1g
$ mkfs.ext4 rootfs.img
$ mkdir rootfs
$ sudo mount -o loop rootfs.img  rootfs
$ cd rootfs
$ sudo cp -r ../busybox/_install/* .
$ sudo mkdir proc sys dev etc etc/init.d

创建init

$ cd etc/init.d/
$ sudo touch rcS
$ sudo vi rcS

编辑该文件内容如下:

#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s

增加权限:

$ sudo chmod +x rcS

最后解除mount

$ sudo umount rootfs

启动riscv64 linux

$ cd riscv64-linux
$ qemu-system-riscv64 -M virt -m 256M -nographic -kernel linux/arch/riscv/boot/Image -drive file=rootfs.img,format=raw,id=hd0  -device virtio-blk-device,drive=hd0 -append "root=/dev/vda rw console=ttyS0"

按回车后即可进入系统:

### 使用QEMU模拟RISC-V架构 #### 安装依赖包 为了确保能够顺利安装并配置QEMU以支持RISC-V架构,在Ubuntu环境中需先更新系统并安装必要的依赖项。这一步骤对于创建稳定的开发环境至关重要[^1]。 ```bash sudo apt update && sudo apt upgrade -y sudo apt install git make gcc build-essential libglib2.0-dev \ zlib1g-dev pkg-config bison flex python3 libpixman-1-dev -y ``` #### 下载与编译QEMU源码 获取最新版的QEMU源代码,并按照官方指南进行编译,特别指定`--target-list=riscv64-softmmu,riscv32-softmmu`参数来启用对RISC-V的支持[^3]。 ```bash git clone https://github.com/qemu/qemu.git cd qemu ./configure --prefix=/usr/local --target-list=riscv64-softmmu,riscv32-softmmu make -j$(nproc) sudo make install ``` #### 获取适用于RISC-V的目标镜像 准备一个适合于RISC-V虚拟机启动的Linux内核和根文件系统镜像。可以从社区资源库下载预构建好的二进制文件,也可以自行编译定制化版本[^4]。 ```bash wget https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x/linux-6.0.tar.gz tar xf linux-6.0.tar.gz cd linux-6.0 make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- defconfig make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -j$(nproc) # 或者直接下载预先制作好的rootfs wget http://example.com/path/to/rootfs.ext4 ``` #### 启动QEMU实例 最后利用上述准备工作完成后的组件,通过命令行启动QEMU模拟器,加载所选的RISC-V CPU模型以及之前准备的操作系统映像[^2]。 ```bash qemu-system-riscv64 \ -machine virt \ -cpu rv64 \ -m 1G \ -bios default \ -kernel ./arch/riscv/boot/Image \ -append "console=ttyS0 rootwait" \ -drive file=rootfs.ext4,format=raw,id=hd0 \ -device virtio-blk-device,drive=hd0,bus=virtio-mmio-bus.0 \ -netdev user,id=user.0 \ -device virtio-net-device,netdev=user.0 \ -serial stdio ``` 此设置将引导进入基于RISC-V的Linux发行版控制台界面,允许进一步执行应用程序测试或其他开发活动。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰灰h

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值