RISC-V on Rust 从零开始(6) - 使用Spike模拟器

其实RISC-V官方也有开发了一个instruction accurate等级的模拟器Spike,只是主要以C++撰写。从提交纪录看起来,这个模拟器已经开发超过十年,支援的指令集也相当丰富,可以作为后续验证的golden。这次尝试从原始码编译整个模拟器,并且实际执行一个简单的RISC-V程式,看看Spike可以提供给我们那些资讯。

安装RISC-V GNU Toolchain

官方已经提供了执行档,下载后将其加入PATH即可。

# Setup RISC-V toolchainwget https://github.com/riscv/riscv-gnu-toolchain/releases/download/2021.06.26/riscv32-elf-ubuntu-20.04-nightly-2021.06.26-nightly.tar.gztar -xvf riscv32-elf-ubuntu-20.04-nightly-2021.06.26-nightly.tar.gzmv riscv riscv-toolchainexport PATH=$(realpath riscv-toolchain/bin):$PATH

编译Proxy Kernel

官方说明如下:

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.

简单来说proxy kernel能将Spike的I/O系统呼叫重新导向host。由于Spike本身没有模拟外部的I/O装置,因此需要proxy kernel才能让这些I/O操作能正常运作。

# Compile the proxy kernelgit clone https://github.com/riscv/riscv-pk.gitcd riscv-pkmkdir buildcd build../configure --prefix=$(realpath ../../riscv-toolchain) --host=riscv32-unknown-elfmakecd ../..

编译Spike模拟器

终于可以编译模拟器本身了,过程与编译proxy kernel类似,只是需要额外安装device-tree-compiler。

# Compile Spike simulatorsudo apt install device-tree-compilergit clone https://github.com/riscv/riscv-isa-sim.gitcd riscv-isa-simmkdir buildcd build../configuremakecd ../..

编译RISC-V程式

程式码

int main() {    return 0;}

编译指令

./riscv-toolchain/bin/riscv32-unknown-elf-gcc -o main.elf main.c

执行Spike

加上-l选项可以看到执行结果,并且指定isa为RV32,才能执行刚刚编译出来的ELF档。

./riscv-isa-sim/build/spike -l --isa=RV32 ./riscv-pk/build/pk main.elf

可以看到以下输出:

core   0: 0x00001000 (0x00000297) auipc   t0, 0x0core   0: 0x00001004 (0x02028593) addi    a1, t0, 32core   0: 0x00001008 (0xf1402573) csrr    a0, mhartidcore   0: 0x0000100c (0x0182a283) lw      t0, 24(t0)core   0: 0x00001010 (0x00028067) jr      t0core   0: 0x80000000 (0x1f80006f) j       pc + 0x1f8core   0: 0x800001f8 (0x00000093) li      ra, 0core   0: 0x800001fc (0x00000113) li      sp, 0core   0: 0x80000200 (0x00000193) li      gp, 0core   0: 0x80000204 (0x00000213) li      tp, 0core   0: 0x80000208 (0x00000293) li      t0, 0...

每行的格式固定,依序为core ID、目前PC address、指令hex、以及相对应的反组译结果。当我们实作完指令decoder之后,就可以使用这个PC trace,来验证我们实作的正确性。


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章