LoginSignup
0
2

【RISC-V・Ubuntu】QemuでRISC-Vシミュレータを構築する

Last updated at Posted at 2023-01-31

背景

 前回までの記事で、IntelのIntrinsic関数をRISC-VやARMにポートする技術のアイディアを書いてきました。

さて、ここで必要になるものがあります。RISC-Vの機械語を動かせるマシンです。この記事では、Intel上でRISC-Vのエミュレータをインストールする方法について記述します。エミュレータは実機とは異なるので、エミュレータを使う場合は、ポートにおけるバグとともに、エミュレータのバグにも対処しなければなりません。その問題はありますが、RISC-Vの実機がなくてもポートの作業ができるということで、それなりの利点はあると思われます。

今回は、Ubuntu 22.01 LTSを使用しました。

インストール

まずは、適当なディレクトリをmkdirで作成し、ディレクトリ内に入って、以下のコマンドを実行します。

以下のコマンドでRISC-Vのqemuをインストール

sudo apt install qemu-system-riscv64

以下のコマンドで、RISC-V版のUbuntuをディレクトリにダウンロードします。

wget https://cdimage.ubuntu.com/releases/22.04/release/ubuntu-22.04.1-live-server-riscv64.img.gz

リンクが効かなくてダウンロードできない場合は、以下のページよりリンクを拾い直してください。
https://cdimage.ubuntu.com/releases/22.04/release/

以下のコマンドで解凍する。

gzip -d ubuntu-22.04.1-live-server-riscv64.img.gz

別のバージョンをダウンロードした場合は、適宜読み替えてください。

以下のコマンドで、設定

qemu-img resize -f raw ubuntu-22.04.1-live-server-riscv64.img +5G
dd if=/dev/zero bs=1M of=disk count=1 seek=16383

以下のコマンドで、OpenSBIを起動

qemu-system-riscv64 \
-machine virt -nographic -m 512 -smp 1 \
-device virtio-rng-pci \
-drive file=ubuntu-22.04.1-live-server-riscv64.img,format=raw,if=virtio

OpenSBIは閉じてしまって構いません。

以下のURLからUbuntu Server 22.04.1をダウンロード
https://ubuntu.com/download/risc-v

以下のコマンドにより、Ubuntu Server 22.04.1を解凍

xz -dv ubuntu-22.04.1-preinstalled-server-riscv64+unmatched.img.xz

以下のコマンドを実行

wget https://cdimage.ubuntu.com/cdimage/ubuntu-server/jammy/daily-live/pending/jammy-live-server-riscv64.img.gz

以下のコマンドを実行

gzip -d jammy-live-server-riscv64.img.gz 

ファームウェアのインストール

sudo apt install u-boot-qemu opensbi

以下のコマンドにより、インストールプロセスが始まります。

/usr/bin/qemu-system-riscv64 -machine virt -m 1G -smp 1 -nographic \
-bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.bin \
-kernel /usr/lib/u-boot/qemu-riscv64_smode/u-boot.bin \
-netdev user,id=net0 \
-device virtio-net-device,netdev=net0 \
-drive file=jammy-live-server-riscv64.img,format=raw,if=virtio \
-drive file=disk,format=raw,if=virtio \
-device virtio-rng-pci

インストールが終わったら、以下のコマンドにより、再び起動します

qemu-system-riscv64 -machine virt -m 1G -smp 1 -nographic \
-bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.bin \
-kernel /usr/lib/u-boot/qemu-riscv64_smode/u-boot.bin \
-netdev user,id=net0,hostfwd=tcp::60022-:22 \
-device virtio-net-device,netdev=net0 \
-drive file=disk,format=raw,if=virtio \
-device virtio-rng-pci

C言語を使いたければ、build-essentialを入れます。

sudo apt install build-essential  
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2