預備知識:C語言、匯編語言、計算機組成原理、操作系統
這個學期開始上操作系統課,於是就來試試 MIT 6.828 的實驗,建議按照 schedule 安排的順序學習。
感覺比我學校的實驗難很多,需要查找並閱讀許多文檔資料(我的實驗記錄里有許多參考資料鏈接可以看看)才能順利完成這些實驗。
官方提供的資料:
- Xv6
- xv6 printout xv6源碼打印出的pdf,沒啥好看的
- xv6 book 實驗過程中遇到不會的不妨看看這本書,比如這本書的附錄B中就對boot loader的代碼有詳細的解釋。
- 閱讀材料
- Reading materials 有很多參考資料
Tools Used in 6.828
- an x86 emulator, QEMU, for running your kernel
- compiler toolchain, including assembler, linker, C compiler, and debugger, for compiling and testing your kernel.
環境配置 Manjaro WSL2
這里提供三種環境配置:
- Manjaro
- Debian
- Ubuntu
每一種都可以使用虛擬機、WSL(Windows Subsystem for Linux)、WSL2。其中由於Ubuntu提供的qemu版本較低,調試功能不是很成熟,需要自己編譯MIT提供的版本,比較麻煩。Debian則需要添加測試版軟件源。
我使用的是 Manjaro WSL2。具體安裝方法見我寫的另一篇文章 Manjaro WSL2 配置記錄(也適用於WSL1)。只需要完成1. 安裝 WSL 之前的配置
、2. 基本安裝
和4. 安裝 xfce4 桌面並啟用 X Server 圖形環境
這三個部分。
然后再安裝我們需要的工具鏈:
sudo pacman -S --needed binutils gcc gdb make git qemu-arch-extra
如果想要使用 Debian 或 Ubuntu,請看下文:
其他可選環境配置
Debian
設置軟件源
sudo nano /etc/apt/sources.list
將原先的內容全部刪除,添加以下內容:
# stable
deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free
# testing
deb http://mirrors.ustc.edu.cn/debian testing main contrib non-free
deb http://mirrors.ustc.edu.cn/debian testing-updates main contrib non-free
將stable軟件源設置為默認:
sudo touch /etc/apt/apt.conf.d/my-default-release
sudo nano /etc/apt/apt.conf.d/my-default-release
添加以下內容:
APT::Default-Release "stable";
安裝編譯工具鏈等
使用以下命令進行安裝:
sudo apt update
sudo apt install git build-essential gdb-multiarch gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu qemu-system-misc/testing
Ubuntu
設置軟件源
sudo nano /etc/apt/sources.list
更換為以下內容
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
然后進行更新
sudo apt update
sudo apt upgrade
安裝編譯工具鏈
# 安裝編譯工具
sudo apt install -y build-essential gdb
# 安裝 32-bit 庫
sudo apt install gcc-multilib
編譯 QEMU
ubuntu提供的qemu版本較低,調試功能不是很成熟,需要自己編譯MIT提供的版本
6.828-qemu
sudo apt install libpixman-1-dev pkg-config zlib1g-dev libglib2.0-dev python
# 文件有點大,建議去github下載壓縮包,
git clone https://github.com/mit-pdos/6.828-qemu.git qemu
cd qemu
./configure --disable-kvm --target-list="i386-softmmu x86_64-softmmu"
這一步可能會報錯。解決辦法見MIT-6.828-JOS-環境搭建。
若成功會輸出一堆東西:
Disabling libtool due to broken toolchain support
Install prefix /usr/local
BIOS directory /usr/local/share/qemu
...--省略--...
之后開始編譯
sudo make && sudo make install
測試一下是否成功
qemu-system-i386 --version
QEMU emulator version 2.3.0, Copyright (c) 2003-2008 Fabrice Bellard