在Ubuntu 20.04上安装了一个RISCV模拟器,记录一下安装的全过程:
从GitHub克隆riscv-toolchain:
git clone https://github.com/riscv/riscv-gnu-toolchain
安装相关依赖库:
sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
进入riscv-gnu-toolchain目录,编译(需要2个多小时)
./configure --prefix=/opt/riscv --enable-multilib
sudo make
sudo make linux
如果后续步骤有问题,分别编译下面
riscv64-unknown-elf-gcc
./configure --prefix=/opt/riscv
sudo make
riscv64-unknown-linux-gnu-gcc
./configure --prefix=/opt/riscv --enable-multilib
sudo make linux
下载riscv-tools
git clone https://github.com/riscv-software-src/riscv-tools
cd ./riscv-tools
安装依赖包
sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev libusb-1.0-0-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev device-tree-compiler pkg-config libexpat-dev
运行
git submodule update --init --recursive
export RISCV=/opt/riscv
./build.sh
如果报错:Please set the RISCV environment variable to your preferred install path
su #切换到root
export RISCV=/opt/riscv
./build-spike-pk.sh
5分钟左右安装成功,提示:
RISC-V Toolchain installation completed!
创建一个代码hello.c
#include <stdio.h>
int main(){
printf("hello world!\n");
return 0;
}
编译
/opt/riscv/bin/riscv64-unknown-elf-gcc -o hello hello.c
9、执行
/opt/riscv/bin/spike pk ./hello
注意: 这里可能会产生一个错误:
bad syscall #90463429510455458
解决方法: 修改riscv-tools/riscv-pk/util/string.c,在memset函数前后添加三行#pragma:
#pragma GCC push_options
#pragma GCC optimize ("no-tree-loop-distribute-patterns")
void* memset(void* dest, int byte, size_t len)
{
if ((((uintptr_t)dest | len) & (sizeof(uintptr_t)-1)) == 0) {
uintptr_t word = byte & 0xFF;
word |= word << 8;
word |= word << 16;
word |= word << 16 << 16;
uintptr_t *d = dest;
while (d < (uintptr_t*)(dest + len))
*d++ = word;
} else {
char *d = dest;
while (d < (char*)(dest + len))
*d++ = byte;
}
return dest;
}
#pragma GCC pop_options
然后进入riscv-tools目录,重新编译./build-spike-pk.sh后,运行/opt/riscv/bin/spike pk ./hello。
参考:
https://www.cnblogs.com/zhaogaojian/p/16536003.html