Ubuntu installs riscv-gnu-toolchain compilation chain and spike, pk usage guide

riscv-gnu-toolchain compilation chain and spike, pk installation and usage guide

1. Environment: Ubuntu18.04

2. Install dependency packages (needed for various compilations):

$ sudo apt 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

3. Differentiate between downloading riscv-gnu-toolchain and riscv-tools

As for why we need to distinguish here, the main reason is that the riscv-gnu-toolchain downloaded from github not only has a compilation chain, but also contains many things about the tool chain, so there are actually many overlaps between the two. For our purpose, we can only use spike. For simulation, just download riscv-gnu-toolchain from github.

4. Download riscv-gnu-toolchain

In order to download as successfully as possible, you can use the following four commands in sequence. Here, you download directly to the current directory. You can create an empty directory for storage and subsequent use to facilitate classification. Of course, the above and following paths must be in English.

git clone https://gitee.com/mirrors/riscv-gnu-toolchain.git #Just download the sub-modules in the framework and there is nothing
cd riscv-gnu-toolchain
git rm --cached qemu #Remove the qemu simulator first, because downloading directly from GitHub is too slow. If you want to use it later, you can download it directly from the official website.
git submodule update --init --recursive #Recursively download submodules

A successful checkout means that the download is complete. You may have to download it multiple times if the network is not good. After downloading, check to make sure there is something in each sub-module. as follows

Submodule path 'glibc': checkout '9826b03b747b841f5fc6de2054bf1ef3f5c4bdf3'

Here is a brief explanation of what is included. As for spike and pk, both riscv-gnu-toolchain and riscv-tools are included, so for the purpose, just download riscv-gnu-toolchain.
-GCC (GNU C Compiler): Convert .c files into executable files on the target machine through “preprocessing-compiling-assembly-linking”
-C runtime library: including glibic, musl, and newlib, which provides function implementation support for the C standard library and can be customized and modified according to needs.
-Binutils:: A set of binary program processing tools, including ar (static library processing), as (assembler), ld (linker), etc.
-GDB: used for project debugging and program running check
-DejaGnu: Program testing framework, providing a front-end support for all tests
-qemu: A purely software-implemented virtualization emulator that can simulate almost any hardware device. If you do not need the emulator, you can delete it before executing the git submodule update –init command: git rm qemu
-spike: a RISC-V emulator that can simulate one or more harts
pk: (proxy kernel) contains a boot loader and a proxy kernel that can handle system calls

5. Compile riscv-gnu-toolchain compilation chain

When the download is completed, one-third of the entire work has been completed. The next step is to compile the RV cross-compilation chain so that it can use the RV architecture to compile the c program to generate an executable file.
Here we first configure the environment variables (that is, set the path) to facilitate subsequent compilation.

$ sudo mkdir /opt/riscv #Here we can create a directory first to store the compiled compilation chain, or we can customize the path, such as: $ sudo mkdir /home/cxc/RISC-V/riscv
$ sudo chmod -R 777 /opt/riscv #Modify the permissions of the directory, otherwise an error may be reported later. Customized ones can also be changed. Check whether there is a lock in the graphical interface directory after compilation. The lock will disappear after modifying the permissions.
$ vim ~/.bashrc
export RISCV="/opt/riscv" #Just put these two sentences at the end of .bashrc. It is the installation path of the tool. After compiling, the compilation chain will exist in this path. The "/opt/riscv" path can be customized such as /home /cxc/RISC-V/riscv
export PATH=$PATH:$RISCV/bin #This path is the link tool
:wq #Save and exit
$ source ~/.bashrc#Update environment

You can use the echo $RISCV command to check whether the path is correct.
After the environment configuration is completed, configure the compilation chain and add the parameter option –enable-multilib to unify the two tools of “riscv32-xxx” and “riscv64-xxx” into the same named file of “riscv64-xxx”, so that you can use 32 You can also use 64-bit compiled C programs for bit architecture compilation, which is convenient for subsequent use. It is best to check the remaining space of the virtual machine before compiling, because the compilation chain of about 15G will be very large, and it is easy to run out of storage space during compilation. How to expand the virtual machine Reference link VMware virtual machine Ubuntu 18.04 data lossless expansion

$ cd riscv-gnu-toolchain
$ mkdir build & amp; & amp; cd build
$ ../configure --prefix=$RISCV --enable-multilib

At this point, the preparatory work for compilation is completed. There are two options for compilation and two compilation chains are generated. Make generates the “riscv64-unknown-elf-” compilation chain using the Newlib library. Here I am using this compilation chain. The GCC tool chain will use newlib as the C runtime library. make linux generates the “riscv64-unknown-linux-gnu-” compilation chain using the Glibc library. The GCC toolchain will use Linux’s Glibc as the C runtime library. The -jn in the command here means that n threads are compiled at the same time to speed up the compilation. Because normal compilation of this compilation chain may take about 30 minutes, so I added this parameter to increase the speed. Generally, the value of n is 2 times of the CPU core, but , it also has something to do with the memory of Ubuntu. Each thread requires at least 1G of memory during compilation. If there is not a lot of memory, just remove it directly.

$ sudo make -jn

or

$ sudo make -jn linux

It will take about 40 minutes to compile depending on the situation. Generally speaking, no compilation error will be reported. The error is usually due to incomplete cloning or permission issues. If there is a permission issue, you can use the sudo chmod command above to modify the permissions, and then make clean to clear the previous compilation. If it fails, make and compile again, but because sudo is used, permission errors should not be reported. After the compilation is completed, you can see the generated compilation tool chain in the defined directory, and then you can compile the c program to generate an executable file. You can just write a c program to test whether the compilation is completed and the executable file is generated.

Vim hello.c

Source code:

#include <stdio.h>

int main() {<!-- -->
    printf("Hello world\
");
    return 0;
}
:wq

The default compilation is 64-bit

$ riscv64-unknown-elf-gcc hello.c -o hello
$ file hello
hello: ELF 64-bit LSB executable, UCB RISC-V, version 1 (SYSV), statically linked, not stripped

If you compile a 32-bit program, you need to specify -march and -mabi. Arch here is the instruction set architecture used for compilation, and abi is the application binary interface, which describes the relationship between the application and the operating system, and between an application and its library. , or low interfaces between components of an application. The option –enable-multilib used in previous compilation includes the following architecture combinations. The ARCH and ABI options cannot be combined in any way. They are only available according to the combination of each line below. For the meaning of the instruction types supported by the specific architecture, please refer to the link Ubuntu installation riscv-gnu-toolchain and riscv-tools

rv32i/ilp32;@march=rv32i@mabi=ilp32
rv32im/ilp32;@march=rv32im@mabi=ilp32
rv32iac/ilp32;@march=rv32iac@mabi=ilp32
rv32imac/ilp32;@march=rv32imac@mabi=ilp32
rv32imafc/ilp32f;@march=rv32imafc@mabi=ilp32f
rv64imac/lp64;@march=rv64imac@mabi=lp64

Then compile for 32-bit, as follows

$ riscv64-unknown-elf-gcc -march=rv32imac -mabi=ilp32 hello.c -o hello
$ file hello
hello: ELF 32-bit LSB executable, UCB RISC-V, version 1 (SYSV), statically linked, not stripped

After entering the command lines 1 and 2, the above prompts lines 3 and 4 will appear. Then when you see the executable file hello in the current directory, the compilation is successful.

6. Compile spike

Spike has source code in riscv-gnu-toolchain, which needs to be compiled and used.

$ apt install device-tree-compiler
$ cd riscv-gnu-toolchain/spike
$ mkdir build
$ cd build
$ ../configure --prefix=/opt/spike #If you want to customize the directory here, use customization such as ../configure --prefix=$RISCV/spike
$ sudo make #sudo can be added or not. If no error is added, try adding it. If you add a prompt here, the error report that the path cannot be found is because echo $RISCV prints out the user's environment variables, but after adding sudo before installation, , the super administrator user is used, and the super administrator user does not set the RISCV environment variable. Solution:
$su
export RISCV=/opt/riscv #Customize and remember to change
exit #Exit as super administrator
$ sudo make

The compilation here is very fast and almost no errors will be reported. After the compilation is completed, use spike –help. If the version information and other parameters are prompted, the compilation is complete. If not, you can use the command sudo make install (I don’t seem to use sudo when compiling. make install will prompt you for version information)
This can actually be used as a bare metal (but no experiments have been conducted). The second option is to use it with pk (agent kernel), which can directly run C programs.

7. Compile pk

This time compilation will be more troublesome, because the downloaded pk needs to distinguish the architecture, it may be 32 or 64, the default is 64 (32 can also be compiled in theory but in fact it cannot be compiled, it may be the version of my pk code Question) The compilation command is as follows

$ cd riscv-gnu-toolchain/pk
$ mkdir build
$ cd build
$ ../configure --prefix=/opt/pk --host=riscv64-unknown-elf
$ make
$ make install

There may be compilation errors here. Because the pk versions are different, the error reports may not be the same. You can search for the error reports directly, such as my error report:

Error: unrecognized opcode `fence.i', extension `zifencei' required

This is because the compile-time architecture instructions do not match. It may be that there is a problem with the abi interface type, so it cannot be compiled. The solution is as follows:

$ make clean #First clean the previously compiled
$ ../configure --prefix=$RISCV --host=riscv64-unknown-elf --with-arch=rv64gc_zifencei
$ make
$ make install

In this way, the compilation can be successful, and the final test directly uses the executable file hello generated before.

$ spike pk hello

Print the following to indicate successful installation.

bbl loader Hello World!

Reference links:
RISCV SOC development environment 1 – download and installation of tool chain
Ubuntu installs riscv-gnu-toolchain and riscv-tools