跳到主要内容

ch1-搭建OS实验环境

信息

前提: 已经安装好rust,可以查看macOS安装Rust

Mac安装QEMU(默认新版v8)

使用brew直接安装qemu,官方下载页面

$ brew install qemu

安装完成之后,控制台输入qemu-tab键尝试补全命令可以看到如下:

$ qemu-
qemu-edid qemu-system-avr qemu-system-mips qemu-system-riscv32 qemu-system-tricore
qemu-img qemu-system-cris qemu-system-mips64 qemu-system-riscv64 qemu-system-x86_64
qemu-io qemu-system-hppa qemu-system-mips64el qemu-system-rx qemu-system-xtensa
qemu-nbd qemu-system-i386 qemu-system-mipsel qemu-system-s390x qemu-system-xtensaeb
qemu-storage-daemon qemu-system-loongarch64 qemu-system-nios2 qemu-system-sh4
qemu-system-aarch64 qemu-system-m68k qemu-system-or1k qemu-system-sh4eb
qemu-system-alpha qemu-system-microblaze qemu-system-ppc qemu-system-sparc
qemu-system-arm qemu-system-microblazeel qemu-system-ppc64 qemu-system-sparc64

默认安装的新版本,并且将所支持的平台都安装了,注意:Qemu在Mac只支持system mode不支持user mode,即只能使用 qemu-system-riscv64 qemu-system-riscv32无法使用 qemu-riscv64 qemu-riscv32linux下支持user mode

查看qemu版本

$ qemu-system-riscv64 --version
QEMU emulator version 8.0.4
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

Ubuntu安装QREMU(版本较老v6)

官方下载页面, 下载的版本较老,v6版本

# 下载system mode程序
sudo apt-get install qemu-system

# 下载user mode程序
sudo apt-get install qemu-user-static

查看版本

$ qemu-system-riscv64 --version
QEMU emulator version 6.2.0 (Debian 1:6.2+dfsg-2ubuntu6.15)
Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers

$ qemu-riscv64-static --version
qemu-riscv64 version 6.2.0 (Debian 1:6.2+dfsg-2ubuntu6.15)
Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers

手动安装QEMUV7版本

卸载已安装的qemu

MacOs

brew uninstall --force qemu

Ubuntu

sudo apt-get remove qemu-system
sudo apt-get remove qemu-user-static

安装编译QEMU所需的依赖项

macOs

brew install automake autoconf libtool glib pkg-config ninja

Ubuntu

sudo apt install -y autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \
gawk build-essential bison flex texinfo gperf libtool patchutils bc ninja-build \
zlib1g-dev libexpat-dev pkg-config libglib2.0-dev libpixman-1-dev git tmux python3

下载源码包并解压

wget https://download.qemu.org/qemu-7.0.0.tar.xz
# 解压
tar xvJf qemu-7.0.0.tar.xz

配置编译QEMU

配置QEMU的编译选项

--prefix指定安装路径,--target-list指定要安装的平台,不指定--target-list默认安装所有平台,

# macOs
./configure --prefix=xxx要安装的路径 --target-list=riscv64-softmmu,riscv32-softmmu

# ubuntu
./configure --prefix=/home/ubuntu/opt/Riscv/qemu --target-list=riscv64-linux-user,riscv32-linux-user,riscv64-softmmu,riscv32-softmmu
  • riscv64-softmmusystem mode对应qemu-system-riscv64
  • riscv32-softmmusystem mode对应qemu-system-riscv32
  • riscv64-linux-useruser mode对应qemu-riscv64 (macOs不支持)
  • riscv32-linux-useruser mode对应qemu-riscv32 ` (macOs不支持)

注:MacOs不支持riscv64-linux-userriscv32-linux-user

编译并安装QEMU:

make -j$(nproc) && make install

配置环境变量

echo 'export QEMU=/home/ubuntu/opt/Riscv/qemu' >> ~/.bashrc && \
echo 'export PATH=$QEMU/bin:$PATH' >> ~/.bashrc && \
source ~/.bashrc

验证安装:运行以下命令来验证QEMU是否正确安装

$ qemu-system-riscv64 --version
QEMU emulator version 7.0.0
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers
$ qemu-riscv64 --version
qemu-riscv64 version 7.0.0
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers

拉取rCore-Tutorial仓库并运行OS

# clone 仓库
git clone https://github.com/LearningOS/rCore-Tutorial-Code-2023A
cd rCore-Tutorial-Code-2023A

# ch1中有一个可运行的hello world程序
git checkout ch1

QemuV8替换RustSBI

rCore-Tutorial-Book中有说明,如果使用的是Qemu8需要使用新RustSBI

下载最新版RustSBI Prereleased 2023-10-27后复制到 bootloader目录下替换同名的rustsbi-qemu.bin 即可,即rCore-Tutorial-Code-2023A/bootloader/rustsbi-qemu.bin

运行os

cd os
LOG=DEBUG make run

结果如下:

(rustup target list | grep "riscv64gc-unknown-none-elf (installed)") || rustup target add riscv64gc-unknown-none-elf
riscv64gc-unknown-none-elf (installed)
cargo install cargo-binutils
Updating crates.io index
Ignored package `cargo-binutils v0.3.6` is already installed, use --force to override
rustup component add rust-src
info: component 'rust-src' is up to date
rustup component add llvm-tools-preview
info: component 'llvm-tools' for target 'x86_64-unknown-linux-gnu' is up to date
Platform: qemu
Finished release [optimized + debuginfo] target(s) in 0.00s
[rustsbi] RustSBI version 0.3.0-alpha.4, adapting to RISC-V SBI v1.0.0
.______ __ __ _______.___________. _______..______ __
| _ \ | | | | / | | / || _ \ | |
| |_) | | | | | | (----`---| |----`| (----`| |_) || |
| / | | | | \ \ | | \ \ | _ < | |
| |\ \----.| `--' |.----) | | | .----) | | |_) || |
| _| `._____| \______/ |_______/ |__| |_______/ |______/ |__|
[rustsbi] Implementation : RustSBI-QEMU Version 0.2.0-alpha.2
[rustsbi] Platform Name : riscv-virtio,qemu
[rustsbi] Platform SMP : 1
[rustsbi] Platform Memory : 0x80000000..0x88000000
[rustsbi] Boot HART : 0
[rustsbi] Device Tree Region : 0x87000000..0x87000ef2
[rustsbi] Firmware Address : 0x80000000
[rustsbi] Supervisor Address : 0x80200000
[rustsbi] pmp01: 0x00000000..0x80000000 (-wr)
[rustsbi] pmp02: 0x80000000..0x80200000 (---)
[rustsbi] pmp03: 0x80200000..0x88000000 (xwr)
[rustsbi] pmp04: 0x88000000..0x00000000 (-wr)
[kernel] Hello, world!
[DEBUG] [kernel] .rodata [0x80202000, 0x80203000)
[ INFO] [kernel] .data [0x80203000, 0x80204000)
[ WARN] [kernel] boot_stack top=bottom=0x80214000, lower_bound=0x80204000
[ERROR] [kernel] .bss [0x80214000, 0x80215000)

需要强制结束Qemu,可以先按下 Ctrl+A ,再按下 X 来退出 Qemu。