欢迎关注“安全有理”微信公众号。
概述
**Nuclei Software Development Kit (SDK)**是芯来推出的开源软件平台,用来加速基于芯来处理器的开发。Nuclei SDK设计架构如下所示:
本文的入门指南均以linux环境进行说明。
工具
安装Nuclei工具链,包括GCC编译器,OpenOCD以及QEMU模拟器。
创建一个目录比如toolchains
,解压上面下载的工具链,注意最终的路径需要类似如下:
${NUCLEI_TOOL_ROOT}
├── gcc
│ ├── bin
│ ├── build.txt
│ ├── gitrepo.txt
│ ├── include
│ ├── lib
│ ├── lib64
│ ├── libexec
│ ├── riscv64-unknown-elf
│ └── share
├── openocd
│ ├── OpenULINK
│ ├── README.md
│ ├── bin
│ ├── contrib
│ ├── distro-info
│ ├── scripts
│ └── share
└── qemu
├── bin
├── doc
├── include
├── libexec
├── readme.md
├── share
└── var
SDK
下载nuclei-sdk,链接如下:
$ git clone https://github.com/Nuclei-Software/nuclei-sdk
在nuclei-sdk顶级目录下创建一个setup_config.sh
脚本,内容为配置工具链的路径:
# This can be path to Nuclei Studio's toolchain folder
NUCLEI_TOOL_ROOT=/path/to/your_tool_root
然后创建nuclei-sdk的编译环境,执行成功会打印工具链跟目录:
$ source setup.sh
Setup Nuclei SDK Tool Environment
NUCLEI_TOOL_ROOT=/path/to/your_tool_root
查看工具链是否准备好:
$ which riscv64-unknown-elf-gcc openocd qemu-system-riscv64
/path/to/your_tool_root/gcc/bin/riscv64-unknown-elf-gcc
/path/to/your_tool_root/openocd/bin/openocd
/path/to/your_tool_root/qemu/bin/qemu-system-riscv64
Hello World
进入nuclei-sdk/application/baremetal/helloworld
,可以执行make help
查看帮助信息:
$ make help
Nuclei N/NX-series RISC-V Embedded Processor Software Development Kit
== For detailed user guide, please check https://doc.nucleisys.com/nuclei_sdk/
== Make variables used in Nuclei SDK ==
SOC: Select SoC built in Nuclei SDK, will select evalsoc by default
BOARD: Select SoC's Board built in Nuclei SDK, will select nuclei_fpga_eval by default
CORE: Not required for all SoCs, currently only evalsoc require it, n307fd by default
ARCH_EXT: Not required for all SoCs, currently only evalsoc require it, such as ARCH_EXT=_zba_zbb_zbc_zbs_xxldspn3x, means B and Nuclei DSP(p v0.5.3) with N1/N2/N3 extension, please use >= gcc 13
DOWNLOAD: Not required for all SoCs, use ilm by default, optional flashxip/ilm/flash/ddr
V: V=1 verbose make, will print more information, by default V=0
支持SOC
、BOARD
、CORE
等变量参数。
以n305
CORE为例,执行编译:
make CORE=n305 DOWNLOAD=ilm all
# Linking : helloworld.elf
# text data bss dec hex filename
# 8756 1272 4592 14620 391c helloworld.elf
Qemu模拟
Nuclei QEMU是基于QEMU 8.x版本进行开发, 支持芯来demosoc
和evalsoc
的评估用SoC。QEMU分为qemu-system-riscv32
和qemu-system-riscv64
,分别对应32位架构的CPU(如n200,n300,n600等系列),和64位架构的CPU(如nx600,nx900等系列)。
qemu常见的命令如下:
# 查看支持的机器类型
$ qemu-system-riscv64 -M ?
# 查看支持的cpu类型
$ qemu-system-riscv64 -M nuclei_evalsoc -cpu ?
模拟helloworld运行的命令如下:
$ qemu-system-riscv32 -M nuclei_evalsoc,download=ilm -cpu nuclei-n205,ext=_zca_zcb_zcmp_zcmt -smp 1 -icount shift=0 -nodefaults -nographic -serial stdio -kernel helloworld.elf
其中ext
表示芯来支持的扩展。
启动成功的话会打印如下:
Nuclei SDK Build Time: Feb 23 2024, 10:42:28
Download Mode: ILM
CPU Frequency 999999078 Hz
CPU HartID: 0
Cluster 0, Hart 0, MISA: 0x40101105
MISA: RV32IMACU
Got rand integer 3278175 using seed 285701249.
0: Hello World From Nuclei RISC-V Processor!
1: Hello World From Nuclei RISC-V Processor!
……
GDB调试
对于qemu的调试,需要加上 -s -S,其含义为:
-S freeze CPU at startup (use 'c' to start execution)
-s shorthand for -gdb tcp::1234
调试hello world示例的命令如下:
$ qemu-system-riscv32 -M nuclei_evalsoc,download=ilm -cpu nuclei-n205,ext=_zca_zcb_zcmp_zcmt -smp 1 -icount shift=0 -nodefaults -nographic -serial stdio -kernel helloworld.elf -s -S
重新打开一个终端,连接tcp端口1234即可,通过riscv64-unknown-elf-gdb
来进行调试。
$ riscv64-unknown-elf-gdb helloworld.elf
连接目标,开始调试:
(gdb) target remote localhost:1234
(gdb) b main
(gdb) c
gdb调试常用命令
命令 | 功能 |
---|---|
set disassemble-next-line on | 打开汇编显示 |
si | 汇编级别单步跳转 |
n | C语言级别单步跳转 |
b | 设置断点 |
info register <register name> | 查看寄存器 |
info register all | 查看所有寄存器 |