riscv simulator
Base environment
Debian
Install Debian
and Terminal
in Microsoft Store. With this app you get Debian for the Windows Subsystem for Linux (WSL). You will be able to use a complete Debian command line environment containing a full current stable release environment.
Modify the source list.
1 | $ sudo vi /etc/apt/sources.list |
Install some library.
1 | $ sudo apt update |
RISCV toolchain
GNU
You can compile riscv toolchain by yourself, but it is not easy and it often has strange issue.
So I highly recommend using the distro.
1 | sudo apt-get install gcc-riscv64-linux-gnu |
If you still want to compile the toolchain, you can refer to below commands.
1 | $ git clone --recursive https://github.com/riscv/riscv-gnu-toolchain |
To build the Newlib cross-compiler, pick an install path. If you choose, say, /opt/riscv
, then add /opt/riscv/bin
to your PATH
now. Then, simply run the following command:
1 | $ ./configure --prefix=/home/francis/opt/riscv |
The riscv64-unknown-elf-gcc
will be installed. The C runtime library used is newlib
.
To build the Linux cross-compiler, pick an install path. If you choose, say, /opt/riscv
, then add /opt/riscv/bin
to your PATH
now. Then, simply run the following command:
1 | $ ./configure --prefix=/home/francis/opt/riscv |
The riscv64-unknown-linux-gcc
will be installed. The C runtime library used is glibc
.
Finally, add path to shrc.
1 | create RISCV /home/francis/opt/riscv |
Spike
Spike, the RISC-V ISA Simulator, implements a functional model of one or more RISC-V harts. It is named after the golden spike used to celebrate the completion of the US transcontinental railway.
1 | $ git clone https://github.com/riscv/riscv-isa-sim.git |
The above command will create $RISCV/bin/spike
.
riscv-pk
The RISC-V Proxy Kernel, pk
, is a lightweight application execution environment that can host statically-linked RISC-V ELF binaries. It is designed to support tethered RISC-V implementations with limited I/O capability and thus handles I/O-related system calls by proxying them to a host computer.
This package also contains the Berkeley Boot Loader, bbl
, which is a supervisor execution environment for tethered RISC-V systems. It is designed to host the RISC-V Linux port.
1 | $ git clone https://github.com/riscv/riscv-pk.git |
The above command will create $RISCV/riscv64-unknown-elf/pk
and bbl
.
hello world on spike
If this test works, it means your tool chain is correctly installed.
1 | $ echo -e '#include <stdio.h>\n int main(void) { printf("Hello world!\\n"); return 0; }' > hello.c |
qemu
There are two version qemu. one is master, it is master version. The other is kvm_qemu, it support the hypervisor v0.6.
If you want master version, you can use this command.
1 | $ git clone https://github.com/qemu/qemu.git |
If you want kvm version, you can use this command.
1 | $ git clone https://github.com/kvm-riscv/qemu.git |
Install command.
1 | $ sudo apt-get install gcc libc6-dev pkg-config bridge-utils uml-utilities zlib1g-dev libglib2.0-dev autoconf automake libtool libsdl1.2-dev |
The above command will create riscv64-softmmu/qemu-system-riscv64
Run Kernel on spike
Download linux
.
1 | $ git clone https://github.com/torvalds/linux.git |
busy box
Download busybox
.
1 | $ curl -L http://busybox.net/downloads/busybox-1.26.2.tar.bz2 >busybox-1.26.2.tar.bz2 |
Enter config menu, and open below configs.
1 | CONFIG_STATIC=y, listed as “Build BusyBox as astatic binary (no shared libs)” in BusyBox Settings Build Options |
1 | $ cd .. |
The above command will create ../linux/rootfs.cpio
kernel
1 | $ cd linux |
The above command will create ../linux/rootfs.cpio
You also can use below command to configure kernel.
1 | $ make -j8 ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- defconfig |
pk
Enter riscv-pk directory, package vmlinux to bbl。
1 | $ cd <riscv-pk>/build |
run kernel
1 | $ spike bbl |
Run KVM on qemu
Currently, we can boot RISC-V 64bit SMP Guest using KVM RISC-V on QEMU. We use QEMU as our development vehicle for RISC-V hypervisor development.
To achieve this, we need following components:
- QEMU with RISC-V Hypervisor Extension Emulation
- OpenSBI Firmware with Hypervisor Extension Support
- Common Host & Guest Kernel
- KVMTOOL
- Host RootFS with KVMTOOL and Guest Kernel
1. Build QEMU with RISC-V Hypervisor Extension Emulation
Note: You must download kvm-riscv/qemu
, NOT qemu master.
1 | git clone https://github.com/kvm-riscv/qemu.git |
The above commands will create ./qemu/riscv64-softmmu/qemu-system-riscv64
which will be our QEMU system emulator.
2. Build OpenSBI Firmware with Hypervisor Extension Support
1 | git clone https://github.com/riscv/opensbi.git |
The above commands will create opensbi/build/platform/generic/firmware/fw_jump.bin
which will be our M-mode runtime firmware.
3. Build Common Host & Guest Linux Kernel Image
We can use same RISC-V 64bit Linux kernel as Guest and Host kernel so no need to compile them separately.
Note: You must download kvm-riscv/linux
, NOT linux master.
1 | git clone https://github.com/kvm-riscv/linux.git |
The above commands will create build-riscv64/arch/riscv/boot/Image
which will be our Guest and Host kernel.
4. Add libfdt library to CROSS_COMPILE SYSROOT directory
We need libfdt library in the cross-compile toolchain for compiling KVMTOOL RISC-V (described in next step). The libfdt library is generally not available in the cross-compile toolchain so we need to explicitly compile libfdt from DTC project and add it to CROSS_COMPILE SYSROOT directory.
1 | git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git |
The above commands will install cross-compiled libfdt library at $SYSROOT/usr/lib64/lp64d directory of cross-compile toolchain.
5. Build KVMTOOL
1 | git clone https://github.com/kvm-riscv/kvmtool.git |
The above commands will create kvmtool/lkvm-static
which will be our user-space tool for KVM RISC-V.
6. Build Host RootFS containing KVMTOOL and Guest Linux
1 | git clone https://github.com/kvm-riscv/howto.git |
The above commands will create rootfs_kvm_riscv64.img
which will be our Host RootFS containing KVMTOOL and Guest Linux.
7. Run RISC-V KVM on QEMU
Run Host Linux with Host RootFS on QEMU
1 | ./qemu/riscv64-softmmu/qemu-system-riscv64 -monitor null -cpu rv64,x-h=true -M virt -m 512M -display none -serial mon:stdio -bios opensbi/build/platform/generic/firmware/fw_jump.bin -kernel ./build-riscv64/arch/riscv/boot/Image -initrd ./rootfs_kvm_riscv64.img -append "root=/dev/ram rw console=ttyS0 earlycon=sbi" |
Run Guest Linux using KVMTOOL after Host Linux is booted
1 | ./apps/lkvm-static run -m 128 -c2 --console serial -p "console=ttyS0 earlycon=uart8250,mmio,0x3f8" -k ./apps/Image --debug |
8. Reference Bootlog
1 | anup@anup-ubuntu64:~/Work/riscv-test/kvm-riscv$ ./qemu/riscv64-softmmu/qemu-system-riscv64 -monitor null -cpu rv64,x-h=true -M virt -m 512M -display none -serial mon:stdio -bios opensbi/build/platform/generic/firmware/fw_jump.bin -kernel ./build-riscv64/arch/riscv/boot/Image -initrd ./rootfs_kvm_riscv64.img -append "root=/dev/ram rw console=ttyS0 earlycon=sbi" |
Run Xvisor on qemu
This section of readme explains how to build & run Xvisor for RISC-V VirtIO board emulated by QEMU.
Please follow the steps below to build & run Linux kernel with Busybox RootFS on Virt64 (paravirtualized RISC-V 64bit) Guest with Xvisor running on QEMU model (v4.2 or higher):
Build Xvisor & DTBs
1 | $ cd <xvisor_source_directory> |
The above commands will create build/vmm.bin
Build Basic Firmware
1 | $ make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -C tests/riscv/virt64/basic |
Build linux kernel
1 | $ cd <linux_source_directory> |
Build rootfs
1 | cp <xvisor_source_directory>/tests/common/busybox/busybox-<busybox_version>_defconfig <busybox_source_directory>/.config |
The above commands will create rootfs.img
Create disk image for Xvisor
1 | $ cd <xvisor_source_directory> |
Launch qemu
1 | $ qemu-system-riscv64 -cpu rv64,x-h=true -M virt -m 512M -display none -serial stdio -bios <opensbi_build_directory>/platform/qemu/virt64/firmware/fw_jump.bin -kernel ./build/vmm.bin -initrd ./build/disk.img -append "vmm.console=uart@10000000 vmm.rtcdev=rtc@101000 vmm.bootcmd=\"vfs mount initrd /;vfs run /boot.xscript;vfs cat /system/banner.txt\"" |
Kick Guest0 to start Basic Firmware
1 | XVisor# guest kick guest0 |
Bind to virtual UART
XVisor# vserial bind guest0/uart0
Copy linux from NOR flash to RAM and start linux booting from RAM
1 | [guest0/uart0] basic# autoexec |
Wait for Linux prompt to come-up and then try out some commands
1 | [guest0/uart0] / # ls |
Enter character seqence ‘ESCAPE+x+q” return to Xvisor prompt]
1 | [guest0/uart0] / # |
Reference
1 | https://github.com/kvm-riscv/howto/wiki/KVM-RISCV64-on-QEMU |