本文寫在安裝成功一天後,由於安裝過程中遇見了極多問題,只能憑藉記憶回想全過程,盡最大努力完善本教程,還請多多見諒!
參考文章/文檔:
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
——到此,所有的安裝已全部完成——