Ubuntu16.04搭建RISCV环境手把手教程(RISCV+spike+gem5+qemu)

本文写在安装成功一天后,由于安装过程中遇见了极多问题,只能凭借记忆回想全过程,尽最大努力完善本教程,还请多多见谅!

参考文章/文档:
1.谨以此写下本人安装riscv的全过程 简单易懂!!(本人环境是在ubantu18.04中)
2.GitHub:riscv-tools
3.GitHub:RISC-V GNU Compiler Toolchain
4.GitHub:Spike RISC-V ISA Simulator
5.GitHub:RISC-V Proxy Kernel and Boot Loader
6.gem5的安装、编译及运行
7.qemu运行riscv linux
8.Documentation/Platforms/RISCV

一、安装RISCV-tools

git clone https://github.com/riscv/riscv-tools.git
cd riscv-tools
git submodule update --init --recursive

二、安装RISCV-toolchain

下载文件:

git clone --recursive https://github.com/riscv/riscv-gnu-toolchain

这一步时间很久,需要科学上网。

cd riscv-gnu-toolchain

安装依赖:

sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev

特别重要的一个依赖!单独说:

sudo apt-get install libnewlib-dev

就是因为这个包没装,报错好几次,哭哭。

安装:

INSTALL_PATH='/opt/riscv'
NUM_CPU=8

export PATH=$PATH:$INSTALL_PATH/BIN
./configure --prefix=$INSTALL_PATH
sudo make -j$NUM_CPU

这里NUM_CPU是虚拟机分配的核的数量,加上之后编译会更快,单独用make也可以。

修改环境变量(为了以后更方便):

sudo gedit ~/.bashrc

在底部添加:

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

编译测试:
写一个hello.c,然后在终端中

riscv64-unknown-elf-gcc -o hello hello.c

如果可以生成可执行文件,则交叉编译环境搭建成功!
(此时还没有riscv的模拟器,因此无法执行)

三、Spike和pk安装

riscv-tools安装中已包含了Spike和pk的相关文件。首先安装Spike(顺序不影响)。
riscv-isa-sim路径下打开终端:

apt-get install device-tree-compiler
 mkdir build
 cd build
 ../configure --prefix=$RISCV
 make
 [sudo] make install

然后安装pk。
riscv-pk路径下打开终端:

 mkdir build
 cd build
 ../configure --prefix=$RISCV --host=riscv64-unknown-elf
 make
 make install

此时该模拟器已完成安装,在可执行文件hello的路径中打开终端,输入:

spike pk hello

输出“hello,world”,安装成功!

四、gem5的安装、编译及运行

安装依赖:

sudo apt install python-six
sudo apt-get install libboost-all-dev

下载文件:

hg clone http://repo.gem5.org/gem5

编译gem5的各个架构:

cd gem5
scons build/RISCV/gem5.opt

这一步时间也比较久,我的虚拟机大概跑了三个小时。

测试:

build/RISCV/gem5.opt configs/example/se.py -c tests/test-progs/hello/bin/riscv/linux/hello

这一步测试的是gem5自带的测试文件,如果需要测试自己写的测试程序:

riscv64-unknown-elf-gcc -o hello hello.c -static
build/RISCV/gem5.opt configs/example/se.py -c hello

注意一定要静态编译!然后测试的时候,我这里终端的地址是在gem5下面的,然后把最后的那个地址参数改成自己的hello文件的地址就行了。

五、qemu的安装、编译及运行

risc-tools/riscv-gnu-toolchain目录下,看到了qemu,所以直接cd进这个目录。

./configure --target-list=riscv64-linux-user && make -j4

-j4是因为自己给虚拟机分配了八个核,然后选了个中间的4。然后这样就完成了!

测试:

./riscv64-linux-user/qemu-riscv64 /home/shensen/RISCV/hello

——到此,所有的安装已全部完成——

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章