bsp: qemu-virt64-riscv: Improve README (#9651)

Improved README document
- Unified the usage process of Standard Edition and Smart Edition.
- Deleted the description of running RTT in Machine Mode. Currently,
  both Standard Edition and Smart Edition RTT run in S-mode by default.

At the same time, some unnecessary scripts were cleaned up.

Signed-off-by: Chen Wang <unicorn_wang@outlook.com>
This commit is contained in:
Chen Wang 2024-11-14 22:09:23 +08:00 committed by GitHub
parent 908dc684b7
commit 83a250f05f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 622 additions and 240 deletions

View File

@ -1,47 +1,318 @@
# RT-Smart QEMU SYSTEM RISC-V RV64 BSP
**QEMU/RISCV64 VIRT Board Support Package User Guide**
English | [中文](./README_ch.md)
English | [中文](./README_cn.md)
## 1. Introduction
<!-- TOC -->
QEMU can emulate both 32-bit and 64-bit RISC-V CPUs. Use the qemu-system-riscv64 executable to simulate a 64-bit RISC-V machine, qemu-system-riscv32 executable to simulate a 32-bit RISC-V machine.
- [1. Introduction](#1-introduction)
- [2. Building](#2-building)
- [2.1. Installing the toolchain](#21-installing-the-toolchain)
- [2.2. Setting RT-Thread toolchain environment variables](#22-setting-rt-thread-toolchain-environment-variables)
- [2.3. Downloading the kernel](#23-downloading-the-kernel)
- [2.4. Configuring the kernel](#24-configuring-the-kernel)
- [2.5. Compiling the kernel](#25-compiling-the-kernel)
- [3. Running](#3-running)
- [3.1. Installing QEMU](#31-installing-qemu)
- [3.2. Running QEMU](#32-running-qemu)
- [4. How to use rv64ilp32 toolchain](#4-how-to-use-rv64ilp32-toolchain)
- [5. Contact information](#5-contact-information)
QEMU has generally good support for RISC-V guests. It has support for several different machines. The reason we support so many is that RISC-V hardware is much more widely varying than x86 hardware. RISC-V CPUs are generally built into “system-on-chip” (SoC) designs created by many different companies with different devices, and these SoCs are then built into machines which can vary still further even if they use the same SoC.
<!-- /TOC -->
For most boards the CPU type is fixed (matching what the hardware has), so typically you dont need to specify the CPU type by hand, except for special cases like the virt board.
# 1. Introduction
## 2. Building
RISC-V is an open and free instruction set architecture (ISA). This project is a port on the RISCV64 VIRT version of QEMU.
It's tedious to properly build a kernel since each RISC-V toolchain is specified to one RISC-V ISA. So you have to use different toolchain for different RISC-V ISAs.
Here we focus on 2 types of ISA: `rv64imafdcv` and `rv64imac`.
This project supports the world's first rv64ilp32 product-level open source toolchain jointly launched by the Xuantie team and the Institute of Software of the Chinese Academy of Sciences.
If you are not sure what kinds of ISA you need, then `rv64imac` should satisfied your case most time. Given a riscv toolchain, you can check the ISA it supports like this:
# 2. Building
```bash
root@a9025fd90fd4:/home/rtthread-smart# riscv64-unknown-linux-musl-gcc -v
Using built-in specs.
COLLECT_GCC=riscv64-unknown-linux-musl-gcc
COLLECT_LTO_WRAPPER=/home/rtthread-smart/tools/gnu_gcc/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/../libexec/gcc/riscv64-unknown-linux-musl/10.1.0/lto-wrapper
Target: riscv64-unknown-linux-musl
Configured with: /builds/alliance/risc-v-toolchain/riscv-gcc/configure --target=riscv64-unknown-linux-musl --prefix=/builds/alliance/risc-v-toolchain/install-native/ --with-sysroot=/builds/alliance/risc-v-toolchain/install-native//riscv64-unknown-linux-musl --with-system-zlib --enable-shared --enable-tls --enable-languages=c,c++ --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libsanitizer --disable-nls --disable-bootstrap --src=/builds/alliance/risc-v-toolchain/riscv-gcc --disable-multilib --with-abi=lp64 --with-arch=rv64imac --with-tune=rocket 'CFLAGS_FOR_TARGET=-O2 -mcmodel=medany -march=rv64imac -mabi=lp64 -D __riscv_soft_float' 'CXXFLAGS_FOR_TARGET=-O2 -mcmodel=medany -march=rv64imac -mabi=lp64 -D __riscv_soft_float'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.1.0 (GCC)
Working system: take Ubuntu 22.04 as an example:
```shell
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy
```
The `-march=***` is what you are looking for. And the `-mabi=***` is also an important message to configure compiling script.
## 2.1. Installing the toolchain
Steps to build kernel:
The specific toolchain used is consistent with the official RT-Thread. For the specific toolchain version, please refer to the file <https://github.com/RT-Thread/rt-thread/blob/master/.github/workflows/action_utest.yml> in the RT-Thread repository.
1. in `$RTT_ROOT/bsp/qemu-virt64-riscv/rtconfig.py:40`, make sure `-march=***` and `-mabi=***` is identical to your toolchain
1. if your -march contains characters v/d/f, then: configure kernel by typing `scons --menuconfig` and select `Using RISC-V Vector Extension` / `Enable FPU`
1. `scons`
```yaml
- name: Install RISC-V ToolChains
if: ${{ matrix.legs.QEMU_ARCH == 'riscv64' && matrix.legs.UTEST != 'rtsmart/riscv64' && success() }}
run: |
wget -q https://github.com/RT-Thread/toolchains-ci/releases/download/v1.4/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz
sudo tar zxvf riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz -C /opt
/opt/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin/riscv64-unknown-elf-gcc --version
echo "RTT_EXEC_PATH=/opt/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin" >> $GITHUB_ENV
## 3. Execution
- name: Install RISC-V Musl ToolChains
if: ${{ matrix.legs.QEMU_ARCH == 'riscv64' && matrix.legs.UTEST == 'rtsmart/riscv64' && success() }}
shell: bash
run: |
wget -q https://github.com/RT-Thread/toolchains-ci/releases/download/v1.7/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu_latest.tar.bz2
sudo tar xjf riscv64-linux-musleabi_for_x86_64-pc-linux-gnu_latest.tar.bz2 -C /opt
/opt/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-gcc --version
echo "RTT_EXEC_PATH=/opt/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin" >> $GITHUB_ENV
echo "RTT_CC_PREFIX=riscv64-unknown-linux-musl-" >> $GITHUB_ENV
```
It's recommended to clone the latest QEMU release and build it locally.
Make sure QEMU is ready by typing `qemu-system-riscv64 --version` in your shell.
Among them, `riscv64-unknown-elf-gcc` is used to build the RT-Thread Standard version, and `riscv64-unknown-linux-musl-gcc` is used to build the RT-Thread Smart version. Download them to your local computer according to the links shown above and decompress them.
Using `qemu-nographic.sh` or `qemu-nographic.bat` to start simulation.
## 2.2. Setting RT-Thread toolchain environment variables
> if your -march contains characters v, using qemu-v-nographic.*
There are three environment variables related to the RT-Thread toolchain
- `RTT_CC` is the toolchain name, which is `"gcc"` here
- `RTT_CC_PREFIX`: is the toolchain prefix, which is `"riscv64-unknown-elf-"` for the Standard version and `"riscv64-unknown-linux-musl-"` for the Smart version.
- `RTT_EXEC_PATH`: the path where the bin folder of the toolchain is located, such as `"$HOME/tools/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin"`. This is set according to the actual path after personal download and decompression. Note that the toolchains of the RT-Thread standard version and the Smart version are two different versions, and the path name of `RTT_EXEC_PATH` must be set to `bin`.
If you use them all the time, it is recommended to export these three environment variables in the `.bashrc` file.
## 2.3. Downloading the kernel
Assume that the working path is `$WORKSPACE`.
```shell
$ cd $WORKSPACE
$ git clone git@github.com:RT-Thread/rt-thread.git
```
**Note: This document is based on the kernel version git commit ID: ebe2926cd6.**
Enter the BSP directory where qemu-virt64-riscv is located. The following operations will not be introduced separately. By default, it is in this directory.
```shell
$ cd $WORKSPACE/rt-thread/bsp/qemu-virt64-riscv
```
## 2.4. Configuring the kernel
Refresh the configuration file before compiling for the first time.
```shell
$ scons --menuconfig
```
The default configuration is the RT-Thread standard version, so if you don't have any special requirements, don't change anything, just save and exit.
If you want to use the RT-Thread Smart version, at least turn on the `RT_USING_SMART` option after entering the configuration menu (see the figure below), and the rest depends on your needs.
```
(Top) → RT-Thread Kernel
RT-Thread Project Configuration
(24) The maximal size of kernel object name
[ ] Use the data types defined in ARCH_CPU
[*] Enable RT-Thread Smart (microkernel on kernel/userland)
[ ] Enable RT-Thread Nano
...
```
Save and exit after modification.
## 2.5. Compiling the kernel
If you have compiled before, you can clean it up:
```shell
$ scons --clean
```
Or compile directly:
```shell
$ scons -j$(nproc)
```
The kernel binary file `rtthread.bin` will be generated in the `$WORKSPACE/rt-thread/bsp/qemu-virt64-riscv`.
# 3. Running
## 3.1. Installing QEMU
```shell
$ sudo apt update
$ sudo apt install qemu-system-misc
```
After the installation is complete, you can check the version.
```shell
$ qemu-system-riscv64 --version
QEMU emulator version 6.2.0 (Debian 1:6.2+dfsg-2ubuntu6.24)
Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers
```
## 3.2. Running QEMU
The repository has provided a ready-made execution script, which can be executed directly:
```shell
$ ./qemu-nographic.sh
```
The running results of the RT-Thread Standard version are as follows:
```shell
$ ./qemu-nographic.sh
OpenSBI v0.9
____ _____ ____ _____
/ __ \ / ____| _ \_ _|
| | | |_ __ ___ _ __ | (___ | |_) || |
| | | | '_ \ / _ \ '_ \ \___ \| _ < | |
| |__| | |_) | __/ | | |____) | |_) || |_
\____/| .__/ \___|_| |_|_____/|____/_____|
| |
|_|
Platform Name : riscv-virtio,qemu
Platform Features : timer,mfdeleg
Platform HART Count : 1
Firmware Base : 0x80000000
Firmware Size : 100 KB
Runtime SBI Version : 0.2
Domain0 Name : root
Domain0 Boot HART : 0
Domain0 HARTs : 0*
Domain0 Region00 : 0x0000000080000000-0x000000008001ffff ()
Domain0 Region01 : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address : 0x0000000080200000
Domain0 Next Arg1 : 0x000000008f000000
Domain0 Next Mode : S-mode
Domain0 SysReset : yes
Boot HART ID : 0
Boot HART Domain : root
Boot HART ISA : rv64imafdcsu
Boot HART Features : scounteren,mcounteren,time
Boot HART PMP Count : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count : 0
Boot HART MHPM Count : 0
Boot HART MIDELEG : 0x0000000000000222
Boot HART MEDELEG : 0x000000000000b109
heap: [0x8028d8a8 - 0x8428d8a8]
\ | /
- RT - Thread Operating System
/ | \ 5.2.0 build Nov 14 2024 15:41:57
2006 - 2024 Copyright by RT-Thread team
lwIP-2.0.3 initialized!
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/utest] utest is initialize success.
[I/utest] total utest testcase num: (0)
file system initialization done!
Hello RISC-V
msh />
```
The running results of RT-Thread Smart version are as follows:
```shell
$ ./qemu-nographic.sh
OpenSBI v0.9
____ _____ ____ _____
/ __ \ / ____| _ \_ _|
| | | |_ __ ___ _ __ | (___ | |_) || |
| | | | '_ \ / _ \ '_ \ \___ \| _ < | |
| |__| | |_) | __/ | | |____) | |_) || |_
\____/| .__/ \___|_| |_|_____/|____/_____|
| |
|_|
Platform Name : riscv-virtio,qemu
Platform Features : timer,mfdeleg
Platform HART Count : 1
Firmware Base : 0x80000000
Firmware Size : 100 KB
Runtime SBI Version : 0.2
Domain0 Name : root
Domain0 Boot HART : 0
Domain0 HARTs : 0*
Domain0 Region00 : 0x0000000080000000-0x000000008001ffff ()
Domain0 Region01 : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address : 0x0000000080200000
Domain0 Next Arg1 : 0x000000008f000000
Domain0 Next Mode : S-mode
Domain0 SysReset : yes
Boot HART ID : 0
Boot HART Domain : root
Boot HART ISA : rv64imafdcsu
Boot HART Features : scounteren,mcounteren,time
Boot HART PMP Count : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count : 0
Boot HART MHPM Count : 0
Boot HART MIDELEG : 0x0000000000000222
Boot HART MEDELEG : 0x000000000000b109
heap: [0x002ef030 - 0x042ef030]
\ | /
- RT - Thread Smart Operating System
/ | \ 5.2.0 build Nov 14 2024 15:48:43
2006 - 2024 Copyright by RT-Thread team
lwIP-2.0.3 initialized!
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/utest] utest is initialize success.
[I/utest] total utest testcase num: (0)
[I/drivers.serial] Using /dev/ttyS0 as default console
file system initialization done!
Hello RISC-V
msh />
```
# 4. How to use rv64ilp32 toolchain
- Toolchain address: <https://github.com/ruyisdk/riscv-gnu-toolchain-rv64ilp32/tags>
- Usage:
- Configure toolchain path
- Modify ABI parameter to: `-mabi=ilp32d`
- Then perform regular compilation
- Use [script](./qemu-rv64ilp32-nographic.sh) to start QEMU (INFO: QEMU binary is also in the toolchain directory)
- Compare the firmware size of the same project compiled using the traditional 64-bit toolchain and the new 32-bit toolchain:
Traditional 64-bit toolchain firmware size:
```bash
Memory region Used Size Region Size %age Used
SRAM: 225856 B 16 MB 1.35%
riscv64-unknown-elf-objcopy -O binary rtthread.elf rtthread.bin
riscv64-unknown-elf-size rtthread.elf
text data bss dec hex filename
150907 3664 71268 225839 3722f rtthread.elf
```
New 32-bit toolchain firmware size:
```bash
Memory region Used Size Region Size %age Used
SRAM: 209376 B 16 MB 1.25%
riscv64-unknown-elf-objcopy -O binary rtthread.elf rtthread.bin
riscv64-unknown-elf-size rtthread.elf
text data bss dec hex filename
138739 1356 69276 209371 331db rtthread.elf
```
# 5. Contact information
Maintainer: [bernard][1]
[1]: https://github.com/BernardXiong

View File

@ -1,181 +0,0 @@
# QEMU/RISCV64 VIRT板级支持包说明
中文页 | [English](README.md)
## 1. 简介
RISC-V是一种开放和免费的指令集体系结构(ISA)。本工程是在QEMU的RISCV64 VIRT版本上进行的一份移植。本工程支持玄铁团队联合中科院软件所共同推出的全球首款rv64ilp32产品级开源工具链。
## 2. 编译说明
首先可以下载交叉编译工具链,建议采用sifive的工具链进行编译。
```
https://www.sifive.com/software
```
选择对应的平台即可。
这里推荐在Ubuntu上进行开发工作。
解压工具链到指定的目录。
```
export RTT_EXEC_PATH=~/gcc/bin
```
进入到`rt-thread/bsp/qemu-virt64-riscv`目录进行输入
```
scons
```
可以看到正常生成`rtthread.elf`与`rtthread.bin`文件。
或者通过 `scons --exec-path="GCC工具链路径"` 命令,在指定工具链位置的同时直接编译。
## 3. 执行
本工程提供了riscv64的两种可配置运行模式,默认运行在M-Mode下。
*M-Mode*
首先安装`qemu-system-riscv64`。
```
sudo apt install qemu-system-misc
```
直接输入
```
./qemu-nographic.sh
```
可以看到程序运行
```
heap: [0x80035804 - 0x86435804]
\ | /
- RT - Thread Operating System
/ | \ 4.0.4 build May 21 2021
2006 - 2021 Copyright by rt-thread team
Hello RISC-V!
msh />
```
*S-Mode*
如果运行在S-Mode下那么需要通过menuconfig选择配置
```
scons --menuconfig
```
选择如下:
```
RISCV qemu virt64 configs --->
[*] RT-Thread run in riscv smode
```
保存后,重新`scons`编译即可。
要让rt-thread运行在S-Mode首先需要启动opensbi,然后通过opensbi启动rt-thread。
自行编译的qemu或者下载的高版本的qemu内置opensbi执行`./qemu-nographic-smode.sh`即可正常运行。
通过`sudo apt install qemu-system-misc`安装的qemu版本较低可自行编译opensbi。
```
git clone git@github.com:riscv/opensbi.git
cd opensbi
make PLATFORM=generic CROSS_COMPILE=~/gcc/bin/riscv64-unknown-elf-
```
最后生成的`/build/platform/generic/firmware/fw_jump.elf`则是需要的文件。
输入以下的命令即可运行:
```
qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin -bios ~/opensbi/build/platform/generic/firmware/fw_jump.elf
```
可以看到如下的结果
```
OpenSBI v0.9
____ _____ ____ _____
/ __ \ / ____| _ \_ _|
| | | |_ __ ___ _ __ | (___ | |_) || |
| | | | '_ \ / _ \ '_ \ \___ \| _ < | |
| |__| | |_) | __/ | | |____) | |_) || |_
\____/| .__/ \___|_| |_|_____/|____/_____|
| |
|_|
Platform Name : riscv-virtio,qemu
Platform Features : timer,mfdeleg
.
.
.
Boot HART ISA : rv64imafdcsu
Boot HART Features : scounteren,mcounteren
Boot HART PMP Count : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count : 0
Boot HART MHPM Count : 0
Boot HART MIDELEG : 0x0000000000000222
Boot HART MEDELEG : 0x000000000000b109
heap: [0x80235a58 - 0x86635a58]
\ | /
- RT - Thread Operating System
/ | \ 4.0.4 build May 21 2021
2006 - 2021 Copyright by rt-thread team
Hello RISC-V!
msh />
```
## 4. 支持情况
| 驱动 | 支持情况 | 备注 |
| ------ | ---- | :------: |
| UART | 支持 | UART0 |
| PLIC | 支持 | - |
| CLIC | 支持 | - |
## 5.如何使用rv64ilp32工具链
1. 工具链地址https://github.com/ruyisdk/riscv-gnu-toolchain-rv64ilp32/tags
2. 使用方法:
- 配置工具链路径
- 修改ABI参数为-mabi=ilp32d
- 然后执行常规编译
- 使用 [脚本](./qemu-rv64ilp32-nographic.sh) 启动 QEMU (INFO: QEMU 二进制同样在工具链目录)
3. 使用传统64位工具链与使用新32位工具链编译相同工程的固件大小对比
传统64位工具链固件大小
```bash
Memory region Used Size Region Size %age Used
SRAM: 225856 B 16 MB 1.35%
riscv64-unknown-elf-objcopy -O binary rtthread.elf rtthread.bin
riscv64-unknown-elf-size rtthread.elf
text data bss dec hex filename
150907 3664 71268 225839 3722f rtthread.elf
```
新32位工具链固件大小
```bash
Memory region Used Size Region Size %age Used
SRAM: 209376 B 16 MB 1.25%
riscv64-unknown-elf-objcopy -O binary rtthread.elf rtthread.bin
riscv64-unknown-elf-size rtthread.elf
text data bss dec hex filename
138739 1356 69276 209371 331db rtthread.elf
```
## 6. 联系人信息
维护人:[bernard][1]
[1]: https://github.com/BernardXiong

View File

@ -1,28 +0,0 @@
# RT-Smart QEMU SYSTEM RISC-V RV64 BSP
中文 | [English](./README.md)
## 1. 简介
本工程是在QEMU System RISC-V 64 模拟器运行,针对 virt 机器的BSP
## 2. 构建
1. 获取 [rt-smart SDK](http://117.143.63.254:9012/www/rt-smart/) risc-v 64版本.
1. 设置 `RTT_EXEC_PATH` 工具链的 bin 文件夹路径, 如 `/home/user/xxx/yyy/bin`
1. 设置 `RTT_CC_PREFIX` 为工具链前缀, 如 `riscv64-unknown-linux-musl-`
1. 设置 `RTT_CC` 为工具链名称, 如 `gcc`
1. 通过指令 `riscv64-unknown-linux-musl-gcc -v` 查看工具链的 -march 与 -mabi
1. 根据相关信息调整本目录下的 `rtconfig.py:40``DEVICE = ' -mcmodel=medany -march=rv64imac -mabi=lp64 '`
1. `scons --menuconfig`
* 如果 `-march` 包含 `v`, 选择 `Using RISC-V Vector Extension`, 反之关闭
* 如果 `-march` 包含 `f/d`, 选择 `Enable FPU`, 反之关闭
1. `scons`
## 3. 运行
推荐拉取最新的 qemu 发行版在本地构建。使用指令确保qemu可用 `qemu-system-riscv64 --version`
使用 `qemu-nographic.sh`/`qemu-nographic.bat` 启动虚拟机。
> if your -march contains characters v, using qemu-v-nographic.*

View File

@ -0,0 +1,321 @@
**QEMU/RISCV64 VIRT 板级支持包使用说明**
中文页 | [English](./README.md)
<!-- TOC -->
- [1. 简介](#1-简介)
- [2. 构建](#2-构建)
- [2.1. 安装工具链](#21-安装工具链)
- [2.2. 设置 RT-Thread 工具链环境变量](#22-设置-rt-thread-工具链环境变量)
- [2.3. 下载内核](#23-下载内核)
- [2.4. 配置内核](#24-配置内核)
- [2.5. 编译内核](#25-编译内核)
- [3. 运行](#3-运行)
- [3.1. 安装 QEMU](#31-安装-qemu)
- [3.2. 运行 QEMU](#32-运行-qemu)
- [4. 如何使用 rv64ilp32 工具链](#4-如何使用-rv64ilp32-工具链)
- [5. 联系人信息](#5-联系人信息)
<!-- /TOC -->
# 1. 简介
RISC-V 是一种开放和免费的指令集体系结构 (ISA)。本工程是在 QEMU 的 RISCV64 VIRT 版本上进行的一份移植。
本工程支持玄铁团队联合中科院软件所共同推出的全球首款 rv64ilp32 产品级开源工具链。
# 2. 构建
工作系统:以 Ubuntu 22.04 为例:
```shell
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy
```
## 2.1. 安装工具链
具体使用的工具链,和 RT-Thread 官方保持一致,具体的工具链版本可以参考 RT-Thread 仓库的 <https://github.com/RT-Thread/rt-thread/blob/master/.github/workflows/action_utest.yml> 这个文件。
```yaml
- name: Install RISC-V ToolChains
if: ${{ matrix.legs.QEMU_ARCH == 'riscv64' && matrix.legs.UTEST != 'rtsmart/riscv64' && success() }}
run: |
wget -q https://github.com/RT-Thread/toolchains-ci/releases/download/v1.4/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz
sudo tar zxvf riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz -C /opt
/opt/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin/riscv64-unknown-elf-gcc --version
echo "RTT_EXEC_PATH=/opt/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin" >> $GITHUB_ENV
- name: Install RISC-V Musl ToolChains
if: ${{ matrix.legs.QEMU_ARCH == 'riscv64' && matrix.legs.UTEST == 'rtsmart/riscv64' && success() }}
shell: bash
run: |
wget -q https://github.com/RT-Thread/toolchains-ci/releases/download/v1.7/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu_latest.tar.bz2
sudo tar xjf riscv64-linux-musleabi_for_x86_64-pc-linux-gnu_latest.tar.bz2 -C /opt
/opt/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin/riscv64-unknown-linux-musl-gcc --version
echo "RTT_EXEC_PATH=/opt/riscv64-linux-musleabi_for_x86_64-pc-linux-gnu/bin" >> $GITHUB_ENV
echo "RTT_CC_PREFIX=riscv64-unknown-linux-musl-" >> $GITHUB_ENV
```
其中 `riscv64-unknown-elf-gcc` 用于构建 RT-Thread 标准版,`riscv64-unknown-linux-musl-gcc` 用于构建 RT-Thread Smart 版。根据上面所示链接分别下载到本地后解压缩。
## 2.2. 设置 RT-Thread 工具链环境变量
和 RT-Thread 工具链相关的环境变量有三个
- `RTT_CC` 为工具链名称, 这里统一为 `"gcc"`
- `RTT_CC_PREFIX`: 为工具链前缀, 这里对于标准版是 `"riscv64-unknown-elf-"`,对于 Smart 版是 `"riscv64-unknown-linux-musl-"`
- `RTT_EXEC_PATH`: 工具链的 bin 文件夹所在路径, 如 `"$HOME/tools/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14/bin"`, 这个根据个人下载解压后的实际路径进行设置,注意 RT-Thread 标准版和 Smart 版本的工具链是两套不同的版本,而且设置 `RTT_EXEC_PATH` 的路径名时要一直到 `bin`
如果一直使用的话,建议将这三个环境变量在 `.bashrc` 文件中 export。
## 2.3. 下载内核
假设工作路径是 `$WORKSPACE`
```shell
$ cd $WORKSPACE
$ git clone git@github.com:RT-Thread/rt-thread.git
```
**注:本文档基于的内核版本 git commit ID 为: ebe2926cd6。**
进入 qemu-virt64-riscv 所在 BSP 目录,后面的操作不做另外介绍,默认就在这个目录下。
```shell
$ cd $WORKSPACE/rt-thread/bsp/qemu-virt64-riscv
```
## 2.4. 配置内核
第一次编译前先刷新一下配置文件。
```shell
$ scons --menuconfig
```
默认配置就是 RT-Thread 标准版,所以如果没有什么特别需求,什么都不要改动,直接保存退出即可。
如果要使用 RT-Thread Smart 版,进入配置菜单后至少要打开 `RT_USING_SMART` 这个选项(见下图),其他的看自己的需求。
```
(Top) → RT-Thread Kernel
RT-Thread Project Configuration
(24) The maximal size of kernel object name
[ ] Use the data types defined in ARCH_CPU
[*] Enable RT-Thread Smart (microkernel on kernel/userland)
[ ] Enable RT-Thread Nano
...
```
修改后保存退出。
## 2.5. 编译内核
如果以前编译后,可以清理一下:
```shell
$ scons --clean
```
或者直接编译:
```shell
$ scons -j$(nproc)
```
`$WORKSPACE/rt-thread/bsp/qemu-virt64-riscv` 路径下会生成内核的二进制文件 `rtthread.bin`
# 3. 运行
## 3.1. 安装 QEMU
```shell
$ sudo apt update
$ sudo apt install qemu-system-misc
```
安装完毕后可以看一下版本。
```shell
$ qemu-system-riscv64 --version
QEMU emulator version 6.2.0 (Debian 1:6.2+dfsg-2ubuntu6.24)
Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers
```
## 3.2. 运行 QEMU
仓库里已经提供了现成的执行脚本,可以直接执行:
```shell
$ ./qemu-nographic.sh
```
RT-Thread 标准版运行结果如下:
```shell
$ ./qemu-nographic.sh
OpenSBI v0.9
____ _____ ____ _____
/ __ \ / ____| _ \_ _|
| | | |_ __ ___ _ __ | (___ | |_) || |
| | | | '_ \ / _ \ '_ \ \___ \| _ < | |
| |__| | |_) | __/ | | |____) | |_) || |_
\____/| .__/ \___|_| |_|_____/|____/_____|
| |
|_|
Platform Name : riscv-virtio,qemu
Platform Features : timer,mfdeleg
Platform HART Count : 1
Firmware Base : 0x80000000
Firmware Size : 100 KB
Runtime SBI Version : 0.2
Domain0 Name : root
Domain0 Boot HART : 0
Domain0 HARTs : 0*
Domain0 Region00 : 0x0000000080000000-0x000000008001ffff ()
Domain0 Region01 : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address : 0x0000000080200000
Domain0 Next Arg1 : 0x000000008f000000
Domain0 Next Mode : S-mode
Domain0 SysReset : yes
Boot HART ID : 0
Boot HART Domain : root
Boot HART ISA : rv64imafdcsu
Boot HART Features : scounteren,mcounteren,time
Boot HART PMP Count : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count : 0
Boot HART MHPM Count : 0
Boot HART MIDELEG : 0x0000000000000222
Boot HART MEDELEG : 0x000000000000b109
heap: [0x8028d8a8 - 0x8428d8a8]
\ | /
- RT - Thread Operating System
/ | \ 5.2.0 build Nov 14 2024 15:41:57
2006 - 2024 Copyright by RT-Thread team
lwIP-2.0.3 initialized!
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/utest] utest is initialize success.
[I/utest] total utest testcase num: (0)
file system initialization done!
Hello RISC-V
msh />
```
RT-Thread Smart 版本运行结果如下:
```shell
$ ./qemu-nographic.sh
OpenSBI v0.9
____ _____ ____ _____
/ __ \ / ____| _ \_ _|
| | | |_ __ ___ _ __ | (___ | |_) || |
| | | | '_ \ / _ \ '_ \ \___ \| _ < | |
| |__| | |_) | __/ | | |____) | |_) || |_
\____/| .__/ \___|_| |_|_____/|____/_____|
| |
|_|
Platform Name : riscv-virtio,qemu
Platform Features : timer,mfdeleg
Platform HART Count : 1
Firmware Base : 0x80000000
Firmware Size : 100 KB
Runtime SBI Version : 0.2
Domain0 Name : root
Domain0 Boot HART : 0
Domain0 HARTs : 0*
Domain0 Region00 : 0x0000000080000000-0x000000008001ffff ()
Domain0 Region01 : 0x0000000000000000-0xffffffffffffffff (R,W,X)
Domain0 Next Address : 0x0000000080200000
Domain0 Next Arg1 : 0x000000008f000000
Domain0 Next Mode : S-mode
Domain0 SysReset : yes
Boot HART ID : 0
Boot HART Domain : root
Boot HART ISA : rv64imafdcsu
Boot HART Features : scounteren,mcounteren,time
Boot HART PMP Count : 16
Boot HART PMP Granularity : 4
Boot HART PMP Address Bits: 54
Boot HART MHPM Count : 0
Boot HART MHPM Count : 0
Boot HART MIDELEG : 0x0000000000000222
Boot HART MEDELEG : 0x000000000000b109
heap: [0x002ef030 - 0x042ef030]
\ | /
- RT - Thread Smart Operating System
/ | \ 5.2.0 build Nov 14 2024 15:48:43
2006 - 2024 Copyright by RT-Thread team
lwIP-2.0.3 initialized!
[I/sal.skt] Socket Abstraction Layer initialize success.
[I/utest] utest is initialize success.
[I/utest] total utest testcase num: (0)
[I/drivers.serial] Using /dev/ttyS0 as default console
file system initialization done!
Hello RISC-V
msh />
```
# 4. 如何使用 rv64ilp32 工具链
- 工具链地址https://github.com/ruyisdk/riscv-gnu-toolchain-rv64ilp32/tags
- 使用方法:
- 配置工具链路径
- 修改ABI参数为`-mabi=ilp32d`
- 然后执行常规编译
- 使用 [脚本](./qemu-rv64ilp32-nographic.sh) 启动 QEMU (INFO: QEMU 二进制同样在工具链目录)
- 使用传统 64 位工具链与使用新 32 位工具链编译相同工程的固件大小对比:
传统 64 位工具链固件大小:
```bash
Memory region Used Size Region Size %age Used
SRAM: 225856 B 16 MB 1.35%
riscv64-unknown-elf-objcopy -O binary rtthread.elf rtthread.bin
riscv64-unknown-elf-size rtthread.elf
text data bss dec hex filename
150907 3664 71268 225839 3722f rtthread.elf
```
新 32 位工具链固件大小:
```bash
Memory region Used Size Region Size %age Used
SRAM: 209376 B 16 MB 1.25%
riscv64-unknown-elf-objcopy -O binary rtthread.elf rtthread.bin
riscv64-unknown-elf-size rtthread.elf
text data bss dec hex filename
138739 1356 69276 209371 331db rtthread.elf
```
# 5. 联系人信息
维护人:[bernard][1]
[1]: https://github.com/BernardXiong

View File

@ -1 +0,0 @@
qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin