# riscv-pke_wsl_env **Repository Path**: BigPants233/riscv-pke_wsl_env ## Basic Information - **Project Name**: riscv-pke_wsl_env - **Description**: HUST(华中科技大学)riscv-pke实验的wsl环境搭建简明教程 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-09-08 - **Last Updated**: 2021-09-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # riscv-pke_wsl_env ## 简介 HUST(华中科技大学)riscv-pke实验的wsl环境搭建简明教程 实验说明和指导文档请参看[这里](https://gitee.com/hustos/pke-doc) 具体说明为本文档, 文件夹 `screenshot` 为一些操作截图 **说明** 独立成行的终端指令中, `$` 表示终端普通用户; `#` 表示终端 `root` ## 准备工作 ### 1 安装 VSCode 为方便进行代码的编辑工作, **建议**安装 `VSCode` (至少对笔者来说对 `vim` 的使用不够熟练) 安装流程请参看[这里](https://zhuanlan.zhihu.com/p/264785441) ### 2 安装 WSL #### 2.1 开启 “适用于Linux的Windows子系统” 功能 1. 进入控制面板 (不知道怎么办的请参看[这里](https://jingyan.baidu.com/article/a501d80c526f7cec630f5ee0.html)) 2. 修改查看方式为“类别”, 点击 “卸载程序” ![01](./screenshot/01.jpg) 3. 点击 “启动或关闭Windows功能”, 找到 “适用于Linux的Windows子系统” 并勾选打开, **重启电脑** ![02](./screenshot/02.jpg) #### 2.2 安装 Ubuntu 1. 进入Microsoft Store 2. 在搜索栏搜索 `Ubuntu` **建议**安装 `Ubuntu 18.04 LTS`, 当然其他稳定发行版亦可 3. 安装完成后, 启动 一种快捷启动方法为 `win+R`, 然后键入 `ubuntu1804`, 可用于同时打开多个终端; 另一种快捷启动方法为将应用固定在任务栏, **好像**只能开启一个终端 4. 首次进入需要稍等几分钟, 设置用户名和密码, 完成后显示 ```bash Installation successful! To run a command as administrator (user "root"), use "sudo ". See "man sudo_root" for details. username@DESKTOP-name:~$ ``` #### 2.3 Ubuntu 配置 #### 2.3.1 设置 root 密码 ```bash username@DESKTOP-name:~$ sudo su - # 使用 sudo 进入 root 用户 [sudo] password for username: root@DESKTOP-name:~ passwd # 修改密码 Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully root@DESKTOP-5TCGC83:~ exit # 退出 exit username@DESKTOP-name:~$ su # 使用设置的root密码进入 Password: root@DESKTOP-name:~ # 登录成功 ``` #### 2.3.2 修改下载源 1. 修改 `/etc/apt/sources.list` `$ sudo sed -i 's#archive.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list` 2. 更新源 `$ sudo apt update` #### 2.3.3 为 Ubuntu 映射网络驱动器 `win + R` 键入 `\\wsl$` 后回车 对 Ubuntu 映射网络驱动器 ![03](./screenshot/03.jpg) 笔者选择的驱动器为 `Z` ![04](./screenshot/04.jpg) 这样就能很方便地在 `文件资源管理器` 中可视化地访问文件 ![05](./screenshot/05.jpg) #### 2.3.4 汉化 没有需要的读者可以**略过** 1. 安装汉化包 `$ sudo apt-get install --assume-yes language-pack-zh-hans` 2. 安装中文字体包 `$ sudo apt-get install --assume-yes fonts-wqy-microhei` 3. 修改语言 `$ sudo echo 'LANG=zh_CN.utf8' > /etc/default/locale` 若操作失败, 请进入 `root` 后重试 4. 重启终端以生效 ### 2.3 下载必要的工具软件 实验在百度网盘上提供了实验涉及的两个工具软件压缩包 `链接: https://pan.baidu.com/s/1cMGt0zWhRidnw7vNUGcZhg 提取码: qbjh` * RISC-V交叉编译器 (riscv-gnu-toolchain-clean.tar.gz) * spike模拟器 (spike-riscv-isa-sim.tar.gz) 在 `RISCV-packages` 目录下, 下载它们 在 `文件资源管理器` 中, 将它们复制到终端的 `/home/username` 即 `~` 目录下 ## 安装实验环境 ### 1 安装 RISC-V交叉编译器 #### 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` #### 1.2 获取RISC-V交叉编译器的源代码 解压缩 `riscv-gnu-toolchain-clean.tar.gz` 即可, 这将花费一点时间 `$ cd ~` `$ tar xf riscv-gnu-toolchain-clean.tar.gz` #### 1.3 构建 (build) RISC-V交叉编译器 `$ cd riscv-gnu-toolchain` `$ ./configure --prefix=[your.RISCV.install.path]` `$ make` 笔者是将交叉编译器安装至系统目录, 需要 `root` 考虑到 `make` 将花费较长时间, **建议**使用 `-j` 开关实现多线程 build 查看 cpu 的线程数 `$ grep 'processor' /proc/cpuinfo | sort -u | wc -l` 笔者的 cpu 的线程数为 `8` 笔者进入目录后构建命令具体如下 `$ su` `# ./configure --prefix=/opt/riscv` `# make -j8` 将耗费较长的时间构建 若出现错误, 请注意 `make error` 报错信息 构建成功后, 退出 `root` `# exit` #### 1.4 设置环境变量 为使每次开启终端后, 环境变量自行设置, **建议**修改 `~/.profile` 或 `/etc/profile` 文件, 前者对当前用户生效, 后者对所有用户生效 笔者选择对 `~/.profile` 文件进行修改 `$ vim ~/.profile` `G` 定位至文件末尾, `A`定位至行尾并进入插入 `INSERT` 模式, 敲击回车后, 键入以下内容(注释就不照抄了吧) ```bash export RISCV=[your.RISCV.install.path] # 笔者这里具体为 export RISCV=/opt/riscv export PATH=$PATH:$RISCV/bin ``` 使文件生效 `$ source ~/.profile` ### 2 安装 spike模拟器 #### 2.1 获取spike模拟器的源代码 解压缩 `spike-riscv-isa-sim.tar.gz` 即可 `$ cd ~` `$ tar xf spike-riscv-isa-sim.tar.gz` #### 2.2 构建 (build) spike 并安装 `$ cd riscv-isa-sim` `$ ./configure --prefix=$RISCV` `$ make` `$ make install` 与该部分 1.3 中的原因类似, 笔者进入目录后构建命令具体如下 `$ su` `# ./configure --prefix=$RISCV` `# make -j8` `# make install` ## 关于实验的 tips ### 1 riscv-pke 实验代码获取 `$ cd ~` `$ git clone https://gitee.com/hustos/riscv-pke.git` 具体文件的含义请参看[这里的2.2节](https://gitee.com/hustos/pke-doc/blob/master/chapter2_installation.md#environments) ### 2 环境验证 `$ cd riscv-pke` `$ make` 将看见以下内容 ```bash 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" ``` `$ spike ./obj/riscv-pke ./obj/app_helloworld` 将看见以下内容 ```bash 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. ``` 如有问题, **请联系老师或助教** 本文档只提供一种搭建实验环境的方法 ### 3 设置实验文件夹为终端默认当前目录 修改 `~/.bashrc` 文件, 在末尾添加 ``` bash cd /home/username/riscv-pke # 这个路径为实验文件夹 ``` 保存修改后, 使修改生效 `$ source ~/.bashrc` 这样每次打开终端就位于实验的工作目录, 方便执行 `make` 或 `git` 相关命令 实验完成后可撤销对 `~/.bashrc` 文件的修改 ### 4 使用 VSCode 添加实验文件夹 1. 打开文件夹 ![06](./screenshot/06.jpg) 2. 选择文件夹 点击左侧的此电脑, 找到 Ubuntu 映射的驱动器, 进入后找到实验文件夹并选择 ![07](./screenshot/07.jpg) 3. 添加后在 VSCode 内编辑代码 效果如下 ![08](./screenshot/08.jpg) ## 后记 恭喜, 历经艰辛后, 现在终于可以开始实验了 实验说明和指导文档请参看[这里](https://gitee.com/hustos/pke-doc) **注意** 本文档只提供一种搭建实验环境的方法 如有问题, **请联系老师或助教**