Skip to content

第一节

yufeng edited this page May 11, 2024 · 1 revision

第一节

本节很简单,需要输出一个 Hello World!

可以使用 template生成,也可以基于 rcore-tutorial-v3进行改造,不过既然是教程,就希望各位跟着一起改造一下rcore-tutorial-v3

Step 1:下载代码

git clone https://github.com/rcore-os/rCore-Tutorial-v3.git
cd rCore-Tutorial-v3
git checkout ch1

Step 2: 运行原版代码

cd os
make run

如果已经正确安装了环境,可以看到下面的输出。

[rustsbi] RustSBI version 0.3.1, 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 : 0x87e00000..0x87e01290
[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!

Step 3: 开始改造依赖项

首先我们需要在 Cargo.toml 里面删除 sbi-rt 的依赖,添加 polyhal 的依赖

polyhal = { git = "https://github.com/Byte-OS/polyhal.git", features = ["kcontext"]}

修改完后的 Cargo.toml 是这个样子。

[package]
name = "os"
version = "0.1.0"
authors = ["Yifan Wu <[email protected]>"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "0.4"
polyhal = { git = "https://github.com/Byte-OS/polyhal.git", features = ["kcontext"]}

[profile.release]
debug = true

Step 4:添加 rust-toolchain.toml 文件

为了保证rust版本一致,我们使用 rust-toolchain.toml来固定我们的rust版本,且自动安装对应的 target。在 os目录下新建文件 rust-toolchain.toml。添加以下内容:

[toolchain]
profile = "minimal"
channel = "nightly-2023-12-01"
components = ["rust-src"]
targets = [
    "riscv64gc-unknown-none-elf",
    "x86_64-unknown-none",
    "aarch64-unknown-none-softfloat",
    "loongarch64-unknown-none"
]

Step 5:删除不需要的代码

由于我们使用 polyhal,我们就不需要关心和架构相关的事情,所以我们需要将这些代码进行剔除。

  1. 删除 os/src/entry.asmos/src/sbi.rs 文件。