1
2# QEMU/RISCV64/C906 VIRT 板级支持包使用说明
3
4中文页 | [English](./README.md)
5
6<!-- TOC -->
7
8- [QEMU/RISCV64/C906 VIRT 板级支持包使用说明](#qemuriscv64c906-virt-板级支持包使用说明)
9  - [1. 简介](#1-简介)
10  - [2. 构建](#2-构建)
11    - [2.1. 安装工具链](#21-安装工具链)
12    - [2.2. 设置 RT-Thread 工具链环境变量](#22-设置-rt-thread-工具链环境变量)
13    - [2.3. 下载内核](#23-下载内核)
14    - [2.4. 配置内核](#24-配置内核)
15    - [2.5. 编译内核](#25-编译内核)
16  - [3. 运行](#3-运行)
17    - [3.1. 安装 QEMU](#31-安装-qemu)
18    - [3.2. 运行 QEMU](#32-运行-qemu)
19      - [3.2.1. 运行 RT-Thread Smart 版](#321-运行-rt-thread-smart-版)
20    - [3.2.2. 运行 RT-Thread Smart 版 + 根文件系统](#322-运行-rt-thread-smart-版--根文件系统)
21  - [4. 联系人信息](#4-联系人信息)
22
23<!-- /TOC -->
24
25## 1. 简介
26
27RISC-V 是一种开放和免费的指令集体系结构 (ISA)。本工程基于 QEMU 的 RISC-V64 VIRT 平台,移植至 C906 处理器平台。
28
29## 2. 构建
30
31工作系统:以 Ubuntu 24.04 为例:
32
33```shell
34$ lsb_release -a
35No LSB modules are available.
36Distributor ID: Ubuntu
37Description:    Ubuntu 24.04.2 LTS
38Release:        24.04
39Codename:       noble
40```
41
42### 2.1. 安装工具链
43
44- 编译器: [Xuantie-900-gcc-linux-6.6.0-musl64-x86_64-V3.0.2-20250410.tar.gz](https://github.com/RT-Thread/toolchains-ci/releases/download/v1.9/Xuantie-900-gcc-linux-6.6.0-musl64-x86_64-V3.0.2.tar.gz)
45- 模拟器: [Xuantie-qemu-x86_64-Ubuntu-20.04-V5.0.5-B20250108-0335.tar.gz](https://www.xrvm.cn/community/download?id=4397435198627713024)
46
47注:若上述链接中的模拟器不能使用,可以使用下述仓库编译模拟器
48> [XUANTIE-RV/qemu](https://github.com/XUANTIE-RV/qemu)
49
50### 2.2. 设置 RT-Thread 工具链环境变量
51
52和 RT-Thread 工具链相关的环境变量有三个
53
54- `RTT_CC` 为工具链名称, 这里统一为 `"gcc"`
55- `RTT_CC_PREFIX`: 为工具链前缀, 这里对于标准版是 `"riscv64-unknown-elf-"`,对于 Smart 版是 `"riscv64-unknown-linux-musl-"`。
56- `RTT_EXEC_PATH`: 工具链的 bin 文件夹所在路径, 如 `"$HOME/tools/Xuantie-900-gcc-linux-6.6.0-musl64-x86_64-V3.0.2/bin"`, 这个根据个人下载解压后的实际路径进行设置,注意 RT-Thread 标准版和 Smart 版本的工具链是两套不同的版本,而且设置 `RTT_EXEC_PATH` 的路径名时要一直到 `bin`。
57
58>注:对于不同的工具链`RTT_CC_PREFIX`可能有所不同,可在工具链 bin 文件夹所在路径下查看
59如果一直使用的话,建议将这三个环境变量在 `.bashrc` 文件中 export。
60
61### 2.3. 下载内核
62
63假设工作路径是 `$WORKSPACE`。
64
65```shell
66cd $WORKSPACE
67git clone git@github.com:RT-Thread/rt-thread.git
68```
69
70进入 xuantie/virt64/c906 所在 BSP 目录,后面的操作不做另外介绍,默认就在这个目录下。
71
72```shell
73cd $WORKSPACE/rt-thread/bsp/xuantie/virt64/c906
74```
75
76### 2.4. 配置内核
77
78第一次编译前先刷新一下配置文件。
79
80```shell
81scons --menuconfig
82```
83
84要使用 RT-Thread Smart 版,进入配置菜单后至少要打开 `RT_USING_SMART` 这个选项(见下图),其他的看自己的需求,如果此处已开启,直接保存退出即可。
85
86```kconfiglib
87(Top) → RT-Thread Kernel
88                                        RT-Thread Project Configuration
89(24) The maximal size of kernel object name
90[ ] Use the data types defined in ARCH_CPU
91[*] Enable RT-Thread Smart (microkernel on kernel/userland)
92[ ] Enable RT-Thread Nano
93...
94```
95
96修改后保存退出。
97
98### 2.5. 编译内核
99
100如果以前编译后,可以清理一下:
101
102```shell
103scons --clean
104```
105
106或者直接编译:
107
108```shell
109scons -j$(nproc)
110```
111
112在 `$WORKSPACE/rt-thread/bsp/xuantie/virt64/c906` 路径下会生成内核的二进制文件 `rtthread.bin`。
113
114## 3. 运行
115
116### 3.1. 安装 QEMU
117
118这里通过编译`XUANTIE-RV/qemu`仓库安装qemu模拟器
119
120```shell
121
122git clone git@github.com:XUANTIE-RV/qemu.git
123
124mkdir build
125
126cd build
127
128../configure --target-list="riscv64-softmmu riscv64-linux-user"
129
130make -j8
131
132make install
133
134```
135
136安装完毕后可以看一下版本。
137
138```shell
139$ qemu-system-riscv64 --version
140QEMU emulator version 8.2.94 (v6.1.0-22774-ge0ace167ef)
141Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
142```
143
144### 3.2. 运行 QEMU
145
146仓库里已经提供了现成的执行脚本,可以直接执行:
147
148```shell
149 ./run.sh
150```
151
152#### 3.2.1. 运行 RT-Thread Smart 版
153
154示例如下:
155
156```shell
157$ ./run.sh
158
159OpenSBI v1.4
160   ____                    _____ ____ _____
161  / __ \                  / ____|  _ \_   _|
162 | |  | |_ __   ___ _ __ | (___ | |_) || |
163 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
164 | |__| | |_) |  __/ | | |____) | |_) || |_
165  \____/| .__/ \___|_| |_|_____/|____/_____|
166        | |
167        |_|
168
169Platform Name             : riscv-virtio,qemu
170Platform Features         : medeleg
171Platform HART Count       : 1
172Platform IPI Device       : aclint-mswi
173Platform Timer Device     : aclint-mtimer @ 10000000Hz
174Platform Console Device   : uart8250
175Platform HSM Device       : ---
176Platform PMU Device       : ---
177Platform Reboot Device    : syscon-reboot
178Platform Shutdown Device  : syscon-poweroff
179Platform Suspend Device   : ---
180Platform CPPC Device      : ---
181Firmware Base             : 0x80000000
182Firmware Size             : 323 KB
183Firmware RW Offset        : 0x40000
184Firmware RW Size          : 67 KB
185Firmware Heap Offset      : 0x48000
186Firmware Heap Size        : 35 KB (total), 2 KB (reserved), 9 KB (used), 23 KB (free)
187Firmware Scratch Size     : 4096 B (total), 328 B (used), 3768 B (free)
188Runtime SBI Version       : 2.0
189
190Domain0 Name              : root
191Domain0 Boot HART         : 0
192Domain0 HARTs             : 0*
193Domain0 Region00          : 0x0000000000100000-0x0000000000100fff M: (I,R,W) S/U: (R,W)
194Domain0 Region01          : 0x0000000010000000-0x0000000010000fff M: (I,R,W) S/U: (R,W)
195Domain0 Region02          : 0x0000000002000000-0x000000000200ffff M: (I,R,W) S/U: ()
196Domain0 Region03          : 0x0000000080040000-0x000000008005ffff M: (R,W) S/U: ()
197Domain0 Region04          : 0x0000000080000000-0x000000008003ffff M: (R,X) S/U: ()
198Domain0 Region05          : 0x000000000c400000-0x000000000c5fffff M: (I,R,W) S/U: (R,W)
199Domain0 Region06          : 0x000000000c000000-0x000000000c3fffff M: (I,R,W) S/U: (R,W)
200Domain0 Region07          : 0x0000000000000000-0xffffffffffffffff M: () S/U: (R,W,X)
201Domain0 Next Address      : 0x0000000080200000
202Domain0 Next Arg1         : 0x000000008fe00000
203Domain0 Next Mode         : S-mode
204Domain0 SysReset          : yes
205Domain0 SysSuspend        : yes
206
207Boot HART ID              : 0
208Boot HART Domain          : root
209Boot HART Priv Version    : v1.10
210Boot HART Base ISA        : rv64imafdc
211Boot HART ISA Extensions  : zicntr,zihpm
212Boot HART PMP Count       : 16
213Boot HART PMP Granularity : 2 bits
214Boot HART PMP Address Bits: 54
215Boot HART MHPM Info       : 16 (0x0007fff8)
216Boot HART MIDELEG         : 0x0000000000000222
217Boot HART MEDELEG         : 0x000000000000b109
218heap: [0x003280c0 - 0x043280c0]
219
220 \ | /
221- RT -     Thread Smart Operating System
222 / | \     5.2.1 build Jun  9 2025 09:03:23
223 2006 - 2024 Copyright by RT-Thread team
224lwIP-2.0.3 initialized!
225[I/sal.skt] Socket Abstraction Layer initialize success.
226[I/utest] utest is initialize success.
227[I/utest] total utest testcase num: (1)
228[I/drivers.serial] Using /dev/ttyS0 as default console
229file system initialization done!
230Hello RISC-V
231msh />
232```
233
234### 3.2.2. 运行 RT-Thread Smart 版 + 根文件系统
235
236对于 Smart 版本的内核,也可以在执行 `run.sh` 脚本时指定根文件系统镜像文件的路径在启动过程中挂载根文件系统。
237
238需要注意的是,内核默认支持 fat, 如果要挂载 ext4 的文件系统,则还需要额外安装 lwext4 软件包,即使能 `PKG_USING_LWEXT4`
239>具体 menuconfig 路径是 (Top) -> RT-Thread online packages -> system packages ->  lwext4: an excellent choice of ext2/3/4 filesystem for microcontrollers
240
241如果在菜单中找不到该软件包,可以退出 menuconfig 并执行 `pkgs --upgrade` 更新软件包索引后再尝试使能软件包。
242勾选该选项后还需要执行如下操作更新软件并安装源码到 bsp 的 packages 目录下(该操作只要执行一次即可):
243
244```shell
245source ~/.env/env.sh
246pkgs --update
247```
248
249保存后重新编译内核。
250
251有关如何制作根文件系统,以及更多详细内容,请参考 <https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-smart/quick-start/qemu-xuantie/quickstart>,这里不再赘述。
252
253示例如下:
254
255```shell
256$ bash run.sh ext4.img
257
258OpenSBI v1.4
259   ____                    _____ ____ _____
260  / __ \                  / ____|  _ \_   _|
261 | |  | |_ __   ___ _ __ | (___ | |_) || |
262 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
263 | |__| | |_) |  __/ | | |____) | |_) || |_
264  \____/| .__/ \___|_| |_|_____/|____/_____|
265        | |
266        |_|
267
268Platform Name             : riscv-virtio,qemu
269Platform Features         : medeleg
270Platform HART Count       : 1
271Platform IPI Device       : aclint-mswi
272Platform Timer Device     : aclint-mtimer @ 10000000Hz
273Platform Console Device   : uart8250
274Platform HSM Device       : ---
275Platform PMU Device       : ---
276Platform Reboot Device    : syscon-reboot
277Platform Shutdown Device  : syscon-poweroff
278Platform Suspend Device   : ---
279Platform CPPC Device      : ---
280Firmware Base             : 0x80000000
281Firmware Size             : 323 KB
282Firmware RW Offset        : 0x40000
283Firmware RW Size          : 67 KB
284Firmware Heap Offset      : 0x48000
285Firmware Heap Size        : 35 KB (total), 2 KB (reserved), 9 KB (used), 23 KB (free)
286Firmware Scratch Size     : 4096 B (total), 328 B (used), 3768 B (free)
287Runtime SBI Version       : 2.0
288
289Domain0 Name              : root
290Domain0 Boot HART         : 0
291Domain0 HARTs             : 0*
292Domain0 Region00          : 0x0000000000100000-0x0000000000100fff M: (I,R,W) S/U: (R,W)
293Domain0 Region01          : 0x0000000010000000-0x0000000010000fff M: (I,R,W) S/U: (R,W)
294Domain0 Region02          : 0x0000000002000000-0x000000000200ffff M: (I,R,W) S/U: ()
295Domain0 Region03          : 0x0000000080040000-0x000000008005ffff M: (R,W) S/U: ()
296Domain0 Region04          : 0x0000000080000000-0x000000008003ffff M: (R,X) S/U: ()
297Domain0 Region05          : 0x000000000c400000-0x000000000c5fffff M: (I,R,W) S/U: (R,W)
298Domain0 Region06          : 0x000000000c000000-0x000000000c3fffff M: (I,R,W) S/U: (R,W)
299Domain0 Region07          : 0x0000000000000000-0xffffffffffffffff M: () S/U: (R,W,X)
300Domain0 Next Address      : 0x0000000080200000
301Domain0 Next Arg1         : 0x000000008fe00000
302Domain0 Next Mode         : S-mode
303Domain0 SysReset          : yes
304Domain0 SysSuspend        : yes
305
306Boot HART ID              : 0
307Boot HART Domain          : root
308Boot HART Priv Version    : v1.10
309Boot HART Base ISA        : rv64imafdc
310Boot HART ISA Extensions  : zicntr,zihpm
311Boot HART PMP Count       : 16
312Boot HART PMP Granularity : 2 bits
313Boot HART PMP Address Bits: 54
314Boot HART MHPM Info       : 16 (0x0007fff8)
315Boot HART MIDELEG         : 0x0000000000000222
316Boot HART MEDELEG         : 0x000000000000b109
317heap: [0x003404c0 - 0x043404c0]
318
319 \ | /
320- RT -     Thread Smart Operating System
321 / | \     5.2.1 build Jun 13 2025 09:43:35
322 2006 - 2024 Copyright by RT-Thread team
323[I/utest] utest is initialize success.
324[I/utest] total utest testcase num: (1)
325[I/drivers.serial] Using /dev/ttyS0 as default console
326[W/DFS.fs] mount / failed with file system type: elm
327file system initialization done!
328Hello RISC-V
329msh />/ #
330/ #
331/ # ls
332bin         lib         proc        sbin        usr
333dev         lost+found  root        services    var
334etc         mnt         run         tmp
335/ #
336```
337
338## 4. 联系人信息
339
340如有意向咨询请联系:<business@rt-thread.com>
341