-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Zhangjin Wu <[email protected]>
- Loading branch information
Showing
9 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
--- | ||
layout: post | ||
author: 'cola2003' | ||
title: '在 QEMU 上运行 xv6' | ||
draft: false | ||
album: 'RISC-V Linux' | ||
license: 'cc-by-nc-nd-4.0' | ||
permalink: /xv6-start-on-qemu/ | ||
description: '在 QEMU 上运行 xv6' | ||
category: | ||
- 开源项目 | ||
- Risc-V | ||
tags: | ||
- Linux | ||
- RISC-V | ||
--- | ||
|
||
> Author: cola2003 <[email protected]> | ||
> Date: 2022/10/06 | ||
> Revisor: Falcon <[email protected]> | ||
> Project: [RISC-V Linux 内核剖析](https://gitee.com/tinylab/riscv-linux) | ||
> Sponsor: SOPHGO | ||
> Proposal: <https://gitee.com/tinylab/riscv-linux/issues/I85LJT> | ||
> Environ: [Linux Lab Disk](https://tinylab.org/linux-lab-disk) | ||
|
||
## 前言 | ||
|
||
xv6 是麻省理工学院开发的经典的教育性操作系统,适合学习操作系统原理和内核开发,它设计简洁,代码清晰,适合深入学习操作系统的内部机制。有助于理解实际操作系统的实现和原理,是学习操作系统的极佳选择。本文将带领大家搭建起 xv6 的 QEMU 实验环境,为进一步的学习与实验打下坚实的基础。 | ||
|
||
## 环境准备 | ||
|
||
这里需要准备的工具有: | ||
- RISC-V 交叉编译工具链 | ||
- QEMU | ||
|
||
如果确实是 Linux 新手或不喜欢无聊的安装过程,想节省这些编译各种工具的造轮子时间,更加专注于实验,可以使用我们泰晓社区的自研产品 [泰晓 Linux 实验盘](https://tinylab.org/linux-lab-disk),在某宝检索 “泰晓 Linux” 即可选购。 | ||
|
||
### 制作交叉编译工具链 | ||
|
||
**说明**:如果采用泰晓 Linux 实验盘,该节的所有繁琐步骤可以完全省略,实验盘中的 Linux Lab 环境提供了完整的 RISC-V 交叉编译器。 | ||
|
||
笔者这里使用的环境是 Ubuntu20.04 LTS x86_64,工具链可以到 GitHub 官网下载源码然后进行编译。 | ||
|
||
``` | ||
git clone https://github.com/riscv-collab/riscv-gnu-toolchain.git | ||
cd riscv-gnu-toolchain | ||
git submodule update --init --recursive | ||
``` | ||
|
||
但实际上我们并不建议这样做,因为 RISC-V 全套工具链太大了,而且因为是在 GitHub 上的缘故所以还需要翻墙,往往会卡在拉取子摸块的阶段,这些子模块包括: | ||
|
||
- riscv-qemu | ||
- riscv-newlib(100MB) | ||
- riscv-binutils(394MB) | ||
- riscv-gdb(394MB) | ||
- riscv-dejagnu(3MB) | ||
- riscv-glibc(155MB) | ||
- riscv-gcc (1.3GB) | ||
|
||
为了解决这个问题,可以拉取码云上的 [riscv-gnu-toolchain 镜像][001]。 | ||
|
||
``` | ||
git clone https://gitee.com/mirrors/riscv-gnu-toolchain | ||
cd riscv-gnu-toolchain | ||
``` | ||
|
||
但是码云上的子模块地址仍然是 GitHub 的地址,所以要分别拉取其子摸块在码云上的镜像。 | ||
|
||
``` | ||
riscv-newlib:https://gitee.com/mirrors/riscv-newlib | ||
riscv-binutils:https://gitee.com/mirrors/riscv-binutils-gdb | ||
riscv-gdb:https://gitee.com/mirrors/riscv-binutils-gdb(riscv-gdb 和 riscv-binutils 为同一个仓库下的不同分支) | ||
riscv-dejagnu:https://gitee.com/mirrors/riscv-dejagnu | ||
riscv-glibc:https://gitee.com/mirrors/riscv-glibc | ||
riscv-gcc:https://gitee.com/mirrors/riscv-gcc | ||
``` | ||
|
||
进行子模块的拉取。 | ||
|
||
``` | ||
git clone https://gitee.com/mirrors/riscv-dejagnu | ||
git clone -b riscv-gcc-10.2.0 https://gitee.com/mirrors/riscv-gcc | ||
git clone -b riscv-glibc-2.29 https://gitee.com/mirrors/riscv-glibc | ||
git clone https://gitee.com/mirrors/riscv-newlib | ||
git clone -b riscv-binutils-2.35 https://gitee.com/mirrors/riscv-binutils-gdb riscv-binutils | ||
git clone -b fsf-gdb-10.1-with-sim https://gitee.com/mirrors/riscv-binutils-gdb riscv-gdb | ||
``` | ||
|
||
我们使用的是 64 位的工具,直接使用下面的命令即可默认生成 64 位的编译工具: | ||
|
||
``` | ||
# 安装依赖库 | ||
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 | ||
# riscv64-unknown-elf-*** | ||
../configure --prefix=$RISCV | ||
make -j4 | ||
# 安装至设定的 $RISCV 路径 | ||
make install | ||
#所需时间较长,请耐心等待 | ||
``` | ||
|
||
最后在 ~/.bashrc 中添加环境变量,也可以使用 export,只是使用 export 环境变量的生效期只为当前终端。 | ||
|
||
``` | ||
# 在 ~/.bashrc 中添加 | ||
export RISCV=".your.path./riscv-gnu-toolchain" | ||
export PATH=$PATH:$RISCV/bin | ||
``` | ||
|
||
完成之后输入 `riscv64-unknown-elf-gcc -v`,若能出现类似如下的界面,则说明编译安装的工具链没有问题。 | ||
|
||
![start-lab-disk](/wp-content/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/test-riscv-tool-chain.png) | ||
|
||
### 准备 QEMU | ||
|
||
**说明**:如果采用泰晓 Linux 实验盘,该节的很多繁琐步骤可以完全省略,实验盘中的 Linux Lab 环境提供了完整的 RISC-V QEMU 模拟器,也可以通过一条 `make qemu` 轻松编译出 QEMU,详细用法可以查看 [第四讲:QEMU 实验 - RISC-V Linux系统开发公开课](https://www.bilibili.com/video/BV1kz4y1K7fW)。 | ||
|
||
本次实验我们使用 qemu7.0.0,要下载 qemu-7.0.0.tar.xz,可以到 [qemu 官网][002] 上去下载,也可以使用下面的 wget 方式下载。 | ||
|
||
![start-lab-disk](/wp-content/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/chose-qemu.jpg) | ||
|
||
``` | ||
wget https://download.qemu.org/qemu-7.0.0.tar.xz | ||
# 对其进行解压缩 | ||
tar -xf qemu-7.0.0.tar.xz | ||
# 安装依赖 | ||
sudo apt-get install libglib2.0-dev ninja-build build-essential zlib1g-dev pkg-config libglib2.0-dev \ | ||
binutils-dev libboost-all-dev autoconf libtool libssl-dev libpixman-1-dev libpython-dev \ | ||
virtualenv libmount-dev libpixman-1-dev | ||
# 然后按照步骤编译安装 | ||
./configure --target-list=riscv64-softmmu | ||
make | ||
sudo make install | ||
``` | ||
|
||
### 准备 xv6 代码 | ||
|
||
克隆官网的代码,进入 xv6-riscv 目录,执行 `make qemu`。 | ||
|
||
``` | ||
git clone https://github.com/mit-pdos/xv6-riscv.git | ||
cd xv6-riscv && make qemu | ||
``` | ||
|
||
![start-lab-disk](/wp-content/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/my-xv6-start.png) | ||
|
||
可以看到我们已经进入 xv6 的 console 了。 | ||
|
||
## 使用泰晓实验盘 | ||
|
||
可以从 [泰晓开源小店](https://shop155917374.taobao.com/) 选购一枚即插即跑的 Linux Lab Disk。它也叫 “泰晓 Linux 实验盘”,可以在淘宝手机 App 内搜索 “泰晓 Linux” 后购买。 | ||
|
||
泰晓 Linux 实验盘的具体使用方法见 [Linux Lab Disk][003]。 | ||
|
||
按照使用方法进入之后界面如下。 | ||
|
||
![start-lab-disk](/wp-content/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/start-lab-disk.png) | ||
|
||
双击 Linux Lab Shell 即可进入实验环境。 | ||
|
||
![start-lab-disk](/wp-content/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/enter-linux-lab.png) | ||
|
||
Linux Lab 里集成了绝大多数我们要用的工具链,具体支持的工具链参见桌面上的手册,输入 RISC-V 后使用 tab 自动补全功能,可以看到我们可以直接使用这些工具,QEMU 也是如此。 | ||
|
||
![start-lab-disk](/wp-content/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/show-tool-chain.png) | ||
|
||
克隆 xv6 代码,进入 xv6-riscv 目录,执行 `make qemu`。 | ||
|
||
|
||
``` | ||
git clone https://github.com/mit-pdos/xv6-riscv.git | ||
cd xv6-riscv && make qemu | ||
``` | ||
|
||
可以看到我们已经进入 xv6 的 console 了。 | ||
|
||
![start-lab-disk](/wp-content/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/xv6-start.png) | ||
|
||
## 总结 | ||
|
||
本篇文章主要是带大家搭建起 xv6 的 QEMU 实验环境,并且尝试使用了我们泰晓社区的自研产品泰晓 Linux 实验盘,为我们进一步的学习打下了坚实的基础。 | ||
|
||
## 参考资料 | ||
|
||
- [riscv-mcu/riscv-gnu-toolchain][001] | ||
- [https://www.qemu.org][002] | ||
- [Linux Lab Disk 使用方法][003] | ||
- [RISCV GNU 编译环境搭建与运行实践][004] | ||
|
||
[001]: https://gitee.com/riscv-mcu/riscv-gnu-toolchain | ||
[002]: https://www.qemu.org/ | ||
[003]: https://tinylab.org/linux-lab-disk/ | ||
[004]: https://blog.csdn.net/ALLap97/article/details/112373544 |
Binary file added
BIN
+53.6 KB
...nt/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/chose-qemu.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+79.6 KB
.../uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/download-xv6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+86.1 KB
...loads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/enter-linux-lab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+168 KB
.../uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/my-xv6-start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+115 KB
...loads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/show-tool-chain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+365 KB
...ploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/start-lab-disk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+233 KB
...2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/test-riscv-tool-chain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+126 KB
...ent/uploads/2022/03/riscv-linux/images/20231006-xv6-start-on-qemu/xv6-start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.