RISC-V V扩展1.0版本环境搭建与测试

Jiaolong...大约 4 分钟RISC-V

0. 准备

在重装了n次后踩的坑

  • 虚拟机:Ubuntu 20.04
  • 内存分配:建议16G+ (或通过分配swap解决)
  • 硬盘分配:建议100G+

1. 下载相关依赖

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

2. 下载安装工具链

下载riscv-gnu-toolchain

git clone -b rvv-next https://github.com/riscv/riscv-gnu-toolchain --progress
cd riscv-gnu-toolchain

这个仓库使用子模块,将自动在需要的时候加载,所以并不需要 --recursive or git submodule update --init --recursive 来一次性下完。

下载riscv-gcc

git submodule update --init --progress  --remote riscv-gcc
cd riscv-gcc
git fetch origin
git checkout origin/riscv-gcc-10.1-rvv-dev

下载riscv-gcc

git submodule update --init --progress  --remote riscv-binutils
cd ../riscv-binutils
git fetch origin
git checkout origin/rvv-1.0.x-zfh

安装

cd riscv-gnu-toolchain
./configure --prefix=/opt/riscv --with-arch=rv64gcv --with-abi=lp64d --with-multilib-generator="rv64gcv-lp64d--"
make -j $(nproc) >make.log 2>&1
make linux -j $(nproc) >make_linux.log 2>&1

如果make -j $(nproc) 失败,试试执行make -j 1

分配空间不足会提示g++: fatal error: Killed signal terminated program cc1plus错误

解决方案:1. g++: fatal error: Killed signal terminated program cc1plus

提示

这一步本人耗时6-7小时,给出本人电脑配置,方便参考时间

处理器:Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz

内存:16G

显卡:GTX 1060 (好像没用到)

中途可能会在部分环节假死好久甚至一两个小时,不用管它,可以用free -h产看内存占用情况

设置环境变量

vim ~/.bashrc

添加如下路径

记得修改为对应路径

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

使环境变量生效

source ~/.bashrc

3. 安装 Spike 和 pk

spike路径下打开终端:

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

pk路径下打开终端:

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

4. 测试编译

  1. 编写简单测试函数add.c

    echo '
    #include <stdio.h>
    int f(int x){
            return x+3;
    }
    int main(){
            printf("hello riscv\n");
            printf("%d\n",f(3));
    }' > add.c
    
  2. 使用riscv64-unknown-elf-gcc编译RISC-V可执行文件

    riscv64-unknown-elf-gcc add.c -o add
    
  3. 使用spike pk模拟运行RISC-V指令

    spike pk add
    
    image-20220808135230132
    image-20220808135230132
  4. 使用riscv64-unknown-elf-gcc生成RISC-V指令的汇编代码

    riscv64-unknown-elf-gcc -S add.c -o add.s
    
    image-20220808135205903
    image-20220808135205903

5. 测试向量指令

  1. 新建汇编代码v_test.s

    #void
    #saxpy(size_t n, const float a, const float *x, float *y)
    # {
    #       size_t i;
    #       for (i=0; i<n; i++)  y[i] = a * x[i] + y[i];
    # }
    #  register arguments:
    #      a0      n
    #      fa0     a
    #      a1      x
    #      a2      y
    .global saxpy
    saxpy:
            vsetvli a4, a0, e32, m8,ta,ma
            vle32.v v0, (a1)
            sub a0, a0, a4
            slli a4, a4, 2
            add a1, a1, a4
            vle32.v v8, (a2)
            vfmacc.vf v8, fa0, v0
            vse32.v v8, (a2)
            add a2, a2, a4
            bnez a0, saxpy
            ret
    
  2. 编译

    riscv64-unknown-elf-as -march=rv64gcv --mabi=lp64d v_test.s -o v_test.o
    
  3. 新建c文件callsaxpy.c

    #include <stddef.h>
    #include <stdio.h>
    
    extern void  saxpy(size_t n, const float a, const float *x, float *y);
    
    int main(){
            size_t size=3;
            const float a=1.0;
            const float array1[]={1.0,2.0,3.0};
            float array2[]={1.0,2.0,3.0};
            for(int i =0;i<size;i++){
                    printf("%f\n", array2[i]);
            }
            printf("----------------\n");
            //调用saxpy函数
            saxpy(size,a,array1,array2);
            for(int i =0;i<size;i++){
                    printf("%f\n", array2[i]);
            }
            return 0;
    }
    
  4. 编译

    riscv64-unknown-elf-gcc -c callsaxpy.c -o callsaxpy.o
    
  5. 生成可执行文件

    riscv64-unknown-elf-gcc -static v_test.o callsaxpy.o -o out
    

或者跳过245直接执行

riscv64-unknown-elf-gcc -g v_test.s callsaxpy.c -o out
  1. 运行

    spike --isa=RV64GCV pk out
    
image-20220815112928349
image-20220815112928349

5. qemu

wget https://download.qemu.org/qemu-7.1.0.tar.bz2
tar -jxvf qemu-7.1.0.tar.bz2
./configure --target-list=riscv64-softmmu,riscv64-linux-user --prefix=/home/jiaolong/riscvv/qemu

报错:ERROR: glib-2.56 gthread-2.0 is required to compile QEMU

目前尝试两种解决办法:

  1. 使用Ubuntu18.04
  2. 手动下载glib

Ubuntu18.04

wget https://download.qemu.org/qemu-7.1.0.tar.bz2
tar -jxvf qemu-7.1.0.tar.bz2
./configure --target-list=riscv64-linux-user,riscv64-softmmu --prefix=/usr/local/qemu
apt-get install libglib2.0-dev
apt-get install libpixman-1-dev

如果想支持rvv则:加上-cpu rv64,v=true

./qemu-riscv64 -cpu rv64,v=true -L $RISCV/sysroot ../../../test/out

手动编译

在尝试apt-get install 安装失败后,使用手动编译

wget http://ftp.acc.umu.se/pub/GNOME/sources/glib/2.56/glib-2.56.4.tar.xz
tar -vxf glib-2.56.4.tar.xz
./configure

再次报错:No package 'libffi' found

继续下载

wget https://github.com/libffi/libffi/releases/download/v3.4.2/libffi-3.4.2.tar.gz
tar -vxf libffi-3.4.2.tar.gz
./configure --prefix=/usr/local/libffi
make
make install
export LIBFFI_LIBS=/usr/local/libffi/lib/libffi.la
export PKG_CONFIG_PATH=/usr/local/libffi/lib/pkgconfig

再次报错:configure: error: *** Could not find libmount

wget http://ftp.ntu.edu.tw/pub/linux/utils/util-linux/v2.38/util-linux-2.38.tar.gz

尝试了一下,搞不定,直接禁用掉--enable-libmount=no

No package 'libpcre' found

PCRE download | SourceForge.netopen in new window

./configure --prefix=/usr/local/pcre --enable-unicode-properties
make
make install
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/pcre/lib/pkgconfig

--enable-unicode-properties

qemu-system-riscv64 \
   -nographic \
   -machine virt \
   -smp 4 \
   -m 2G \
   -kernel Fedora-Minimal-Rawhide-*-fw_payload-uboot-qemu-virt-smode.elf \
   -bios none \
   -object rng-random,filename=/dev/urandom,id=rng0 \
   -device virtio-rng-device,rng=rng0 \
   -device virtio-blk-device,drive=hd0 \
   -drive file=Fedora-Minimal-Rawhide-20200108.n.0-sda.raw,format=raw,id=hd0 \
   -device virtio-net-device,netdev=usernet \
   -netdev user,id=usernet,hostfwd=tcp::10000-:22 \
   -fsdev local,security_model=passthrough,id=fsdev0,path=/home/jiaolong/riscvv/hostshare \
   -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare

报错记录

1. g++: fatal error: Killed signal terminated program cc1plus




参考链接:

  1. RISC-V “V” 拓展—1.0版本环境搭建与测试_riscv_life的博客-CSDN博客open in new window
  2. Ubuntu16.04搭建RISCV环境手把手教程(RISCV+spike+gem5+qemu)-CSDN博客open in new window
  3. C++: fatal error: Killed signal terminated program cc1plus_tiffiny10的博客-CSDN博客open in new window