# riscv **Repository Path**: alek-ziyi/hex ## Basic Information - **Project Name**: riscv - **Description**: riscv操作系统实验 - **Primary Language**: C/C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-06-06 - **Last Updated**: 2025-01-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README PS:本系列实验在ubuntu普通用户模式下进行,**不需要切换到root用户** ## 一、环境配置 **配置环境前确保ubuntu虚拟机网络正常且镜像源和```git```命令能够正常使用**,镜像源配置可参考清华源官方教程:[清华大学开源软件镜像站](https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/) ### 1_1. 安装依赖库 RISC-V交叉编译器的构建需要一些本地支撑软件包,使用以下命令安装 ``` $ sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev device-tree-compiler vim ``` ### 1_2. 下载自包含交叉编译器 平台已提供打包好的编译器供使用,浏览器链接 下载解压后的文件要保存在固定位置,这里保存在**用户根目录** 解压文件 ``` $ cd ~ $ tar xf riscv64-elf-gcc-20210923.tgz ``` ### 1_3. 配置环境变量 使用任意文本编辑器打开文件```~/.bashrc```,这里使用vim编辑器,按```i```进入编辑模式 ``` $ vim ~/.bashrc ``` 自行替换```<用户名>```,将以下命令追加到```~/.bashrc```文件中。修改完成后按```Esc```退出编辑模式,输入```:wq```保存并退出 ``` export RISCV=/home/<用户名>/riscv64-elf-gcc export PATH=$PATH:$RISCV/bin ``` 以上命令设置了交叉编译器的PATH路径,如果放到了其它路径需自行替换```export RISCV=```后面的绝对路径 配置完成后重启命令行,输入以下命令验证环境,将看到以下输出 ``` $ cd ~ $ riscv64-unknown-elf-gcc riscv64-unknown-elf-gcc: fatal error: no input files compilation terminated. ``` ### 1_4. 下载实验代码 在用户根目录新建文件夹```riscv```作为实验文件夹 ``` $ mkdir riscv $ cd riscv ``` 克隆实验代码仓库 ``` $ git clone https://gitee.com/hustos/riscv-pke.git Cloning into 'riscv-pke-prerelease'... remote: Enumerating objects: 195, done. remote: Counting objects: 100% (195/195), done. remote: Compressing objects: 100% (195/195), done. remote: Total 227 (delta 107), reused 1 (delta 0), pack-reused 32 Receiving objects: 100% (227/227), 64.49 KiB | 335.00 KiB/s, done. Resolving deltas: 100% (107/107), done. ``` 编译内核,成功将看到以下输出 ``` $ cd riscv-pke $ make compiling util/snprintf.c compiling util/string.c linking obj/util.a ... Util lib has been build into "obj/util.a" compiling spike_interface/dts_parse.c compiling spike_interface/spike_htif.c compiling spike_interface/spike_utils.c compiling spike_interface/spike_file.c compiling spike_interface/spike_memory.c linking obj/spike_interface.a ... Spike lib has been build into "obj/spike_interface.a" compiling kernel/syscall.c compiling kernel/elf.c compiling kernel/process.c compiling kernel/strap.c compiling kernel/kernel.c compiling kernel/machine/minit.c compiling kernel/strap_vector.S compiling kernel/machine/mentry.S linking obj/riscv-pke ... PKE core has been built into "obj/riscv-pke" compiling user/app_helloworld.c compiling user/user_lib.c linking obj/app_helloworld ... User app has been built into "obj/app_helloworld" ``` ### 1_5. 环境验证 ```cd ~/riscv/riscv-pke```进入实验目录 输入以下命令启动内核,将可执行文件```./obj/app_helloworld```挂载到内核中,输出日志与下文相同则环境配置成功 ``` $ spike ./obj/riscv-pke ./obj/app_helloworld In m_start, hartid:0 HTIF is available! (Emulated) memory size: 2048 MB Enter supervisor mode... Application: ./obj/app_helloworld Application program entry point (virtual address): 0x0000000081000000 Switching to user mode... call do_syscall to accomplish the syscall and lab1_1 here. System is shutting down with exit code -1. ``` ## 二、实验前准备 ### 2_1. 项目目录结构 进入环境目录 ``` $ cd ~/riscv/riscv-pke $ tree ├── LICENSE.txt ├── Makefile # make构建脚本 ├── README.md ├── kernel # 内核文件目录 │ ├── config.h │ ├── elf.c │ ├── elf.h # 处理elf可执行文件的代码 │ ├── kernel.c # 内核代码,定义s_start函数,设置陷阱向量以实现系统调用的自动跳转,初始化进程管理系统以及启动用户进程 │ ├── kernel.lds # 链接过程的定义脚本 │ ├── machine │ │ ├── mentry.S # 操作系统入口的汇编代码,执行处理器初始化后跳转到minit.c的m_start │ │ └── minit.c # 定义m_start函数,用于操作系统启动前的初始化,初始化完成后将跳转到kernel.c中的s_start │ ├── process.c │ ├── process.h # 进程相关实现 │ ├── riscv.h # 定义了riscv架构下操作寄存器的宏命令的标准库,用于代码与底层的交互 │ ├── strap.c │ ├── strap.h # 陷阱向量指向此.h里的函数,陷阱命中时调用syscall.h,完成系统调用后重新启动进程 │ ├── strap_vector.S # 陷阱处理相关的汇编 │ ├── syscall.c │ └── syscall.h # syscall实现了系统调用,调用spike_interface中的代码实现文件和print相关功能 ├── spike_interface # 与spike模拟器交互的相关标准库,用于补全操作系统功能。 │ ├── atomic.h │ ├── dts_parse.c │ ├── dts_parse.h │ ├── spike_file.c │ ├── spike_file.h │ ├── spike_htif.c │ ├── spike_htif.h │ ├── spike_memory.c │ ├── spike_memory.h │ ├── spike_utils.c │ └── spike_utils.h ├── user # 用户端代码 │ ├── app_helloworld.c │ ├── user.lds │ ├── user_lib.c │ └── user_lib.h └── util # 包含字符串处理等工具函数 ├── functions.h ├── load_store.S ├── snprintf.c ├── snprintf.h ├── string.c ├── string.h └── types.h ``` ### 2_2. 官方文档 项目页面: 实验文档: 环境配置: