Articles

第四章:地址空间 · rCore 2024S 随记

唉。他妈的排课,都没什么时间做 rCore 了。我现在学校的课程从早 8 排到晚 22。简直是高中。 前言 实际实践参考的文档:rCore Tutorial Guide 2024S。 拥有更为详细细节介绍的 rCore OS 官方文档:rCore-Tutorial-Book-v3。 第四章:地址空间 引言 试着运行本章代码。 $ git checkout ch4 Already on 'ch4' Your branch is up to date with 'origin/ch4'. ~/2024s-rcore-255doesnotexist$ cd os ~/2024s-rcore-255doesnotexist/os$ 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 make[1]: Entering directory '/home/ezra/2024s-rcore-255doesnotexist/user' Removed 1342 files, 15.

5月7日随记:天杀的哪个b排的课早上8点上到晚上22点

如题。今天他娘的一点都没有空闲干别的。 ADD 05/09/2024: 还是一样。rCore Lab 都没空做了。

第三章:多道程序与分时多任务 · rCore 2024S 随记

本部分将试着毛估估的完成 ch3。由于本人同时在学习 RISC-V 汇编,涉及的内容可能解释的不那么详细。 前言 实际实践参考的文档:rCore Tutorial Guide 2024S。 拥有更为详细细节介绍的 rCore OS 官方文档:rCore-Tutorial-Book-v3。 第三章:多道程序与分时多任务 引言 概述 本章的目标是实现分时多任务系统,它能并发地执行多个用户程序,并调度这些程序。为此需要实现: 一次性加载所有用户程序,减少任务切换开销; 支持任务切换机制,保存切换前后程序上下文; 支持程序主动放弃处理器,实现 yield 系统调用; 以时间片轮转算法调度用户程序,实现资源的时分复用。 在 qemu 模拟器上运行本章代码: $ cd os $ make run 运行代码,看到用户程序交替输出信息: $ 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 make[1]: Entering directory '/home/ezra/2024s-rcore-255doesnotexist/user' Removed 138 files, 3.

第二章:批处理系统 · rCore 2024S 随记

由于 ch2 已经被实现好了,本部分基本为摘抄。 前言 实际实践参考的文档:rCore Tutorial Guide 2024S。 拥有更为详细细节介绍的 rCore OS 官方文档:rCore-Tutorial-Book-v3。 第二章:应用程序与基本执行环境 引言 批处理系统 (Batch System) 出现于计算资源匮乏的年代,其核心思想是: 将多个程序打包到一起输入计算机;当一个程序运行结束后,计算机会 自动 执行下一个程序。 实践体验 本章我们引入了用户程序。为了将内核与应用解耦,我们将二者分成了两个仓库,分别是存放内核程序的 rCore-Tutorial-Code-20xxx (下称代码仓库,最后几位 x 表示学期)与存放用户程序的 rCore-Tutorial-Test-20xxx (下称测例仓库)。 你首先需要进入代码仓库文件夹并 clone 用户程序仓库(如果已经执行过该步骤则不需要再重复执行): $ git clone https://github.com/LearningOS/rCore-Tutorial-Code-2024S.git $ cd rCore-Tutorial-Code-2024S $ git checkout ch2 $ git clone https://github.com/LearningOS/rCore-Tutorial-Test-2024S.git user 上面的指令会将测例仓库克隆到代码仓库下并命名为 user ,注意 /user 在代码仓库的 .gitignore 文件中,因此不会出现 .git 文件夹嵌套的问题,并且你在代码仓库进行 checkout 操作时也不会影响测例仓库的内容。 在 qemu 模拟器上运行本章代码: $ cd os $ make run LOG=INFO (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.

第一章:应用程序与基本执行环境 · rCore 2024S 随记

前言 实际实践参考的文档:rCore Tutorial Guide 2024S。 拥有更为详细细节介绍的 rCore OS 官方文档:rCore-Tutorial-Book-v3。 第一章:应用程序与基本执行环境 引言 本章预期代码树: ├── bootloader (内核依赖的运行在 M 特权级的 SBI 实现,本项目中我们使用 RustSBI) │ └── rustsbi-qemu.bin ├── os │ ├── Cargo.toml (cargo 项目配置文件) │ ├── Makefile │ └── src │ ├── console.rs (将打印字符的 SBI 接口进一步封装实现更加强大的格式化输出) │ ├── entry.asm (设置内核执行环境的的一段汇编代码) │ ├── lang_items.rs (需要我们提供给 Rust 编译器的一些语义项,目前包含内核 panic 时的处理逻辑) │ ├── linker.ld (控制内核内存布局的链接脚本以使内核运行在 qemu 虚拟机上) │ ├── logging.rs (为本项目实现了日志功能) │ ├── main.rs (内核主函数) │ └── sbi.

第零章:实验环境配置 · rCore 2024S 随记

前言 实际实践参考的文档:rCore Tutorial Guide 2024S。 拥有更为详细细节介绍的 rCore OS 官方文档:rCore-Tutorial-Book-v3。 第零章:实验环境配置 OS 环境配置 OS环境 Ubuntu Server 22.04 amd64。 Rust 开发环境配置 通过 rustup 安装的当前最新版工具链。 curl https://sh.rustup.rs -sSf | sh Qemu 模拟器安装 参考文档,从源码安装 Qemu 7.0.0。 sudo apt 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 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 # 编译安装并配置 RISC-V 支持 cd qemu-7.

Hello

Hello.

第四章:地址空间 · rCore 2024S 随记

Planted May 11, 2024

  • 唉。他妈的排课,都没什么时间做 rCore 了。我现在学校的课程从早 8 排到晚 22。简直是高中。

前言

实际实践参考的文档:rCore Tutorial Guide 2024S

拥有更为详细细节介绍的 rCore OS 官方文档:rCore-Tutorial-Book-v3

第四章:地址空间

引言

试着运行本章代码。

$ git checkout ch4
Already on 'ch4'
Your branch is up to date with 'origin/ch4'.
~/2024s-rcore-255doesnotexist$ cd os
~/2024s-rcore-255doesnotexist/os$ 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
make[1]: Entering directory '/home/ezra/2024s-rcore-255doesnotexist/user'
     Removed 1342 files, 15.6MiB total
target/riscv64gc-unknown-none-elf/release/ch2b_bad_address target/riscv64gc-unknown-none-elf/release/ch2b_bad_instructions target/riscv64gc-unknown-none-elf/release/ch2b_bad_register target/riscv64gc-unknown-none-elf/release/ch2b_hello_world target/riscv64gc-unknown-none-elf/release/ch2b_power_3 target/riscv64gc-unknown-none-elf/release/ch2b_power_5 target/riscv64gc-unknown-none-elf/release/ch2b_power_7 target/riscv64gc-unknown-none-elf/release/ch3b_yield0 target/riscv64gc-unknown-none-elf/release/ch3b_yield1 target/riscv64gc-unknown-none-elf/release/ch3b_yield2 target/riscv64gc-unknown-none-elf/release/ch4b_sbrk
   Compiling scopeguard v1.2.0
   Compiling spin v0.5.2
   Compiling spin v0.7.1
   Compiling bitflags v1.3.2
   Compiling lock_api v0.4.6
   Compiling lazy_static v1.4.0
   Compiling buddy_system_allocator v0.6.0
   Compiling spin v0.9.8
   Compiling user_lib v0.1.0 (/home/ezra/2024s-rcore-255doesnotexist/user)
warning: creating a mutable reference to mutable static is discouraged
  --> src/bin/ch8b_race_adder.rs:18:17
   |
18 |         let a = &mut A as *mut usize;
   |                 ^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
   = note: this will be a hard error in the 2024 edition
   = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
   = note: `#[warn(static_mut_refs)]` on by default
help: use `addr_of_mut!` instead to create a raw pointer
   |
18 |         let a = addr_of_mut!(A) as *mut usize;
   |                 ~~~~~~~~~~~~~~~

warning: unused import: `get_time`
  --> src/bin/ch7b_pipe_large_test.rs:10:29
   |
10 | use user_lib::{close, fork, get_time, pipe, read, wait, write,getpid};
   |                             ^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: creating a mutable reference to mutable static is discouraged
  --> src/bin/ch8b_race_adder_atomic.rs:26:17
   |
26 |         let a = &mut A as *mut usize;
   |                 ^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
   = note: this will be a hard error in the 2024 edition
   = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
   = note: `#[warn(static_mut_refs)]` on by default
help: use `addr_of_mut!` instead to create a raw pointer
   |
26 |         let a = addr_of_mut!(A) as *mut usize;
   |                 ~~~~~~~~~~~~~~~

warning: `user_lib` (bin "ch8b_race_adder") generated 1 warning
warning: creating a mutable reference to mutable static is discouraged
  --> src/bin/ch8b_race_adder_mutex_spin.rs:20:17
   |
20 |         let a = &mut A as *mut usize;
   |                 ^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
   = note: this will be a hard error in the 2024 edition
   = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
   = note: `#[warn(static_mut_refs)]` on by default
help: use `addr_of_mut!` instead to create a raw pointer
   |
20 |         let a = addr_of_mut!(A) as *mut usize;
   |                 ~~~~~~~~~~~~~~~

warning: Rust ABI is unsupported in naked functions
   --> src/bin/ch8b_stackful_coroutine.rs:259:1
    |
259 | unsafe fn switch(old: *mut TaskContext, new: *const TaskContext) {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(undefined_naked_function_abi)]` on by default

warning: creating a mutable reference to mutable static is discouraged
  --> src/bin/ch8b_race_adder_loop.rs:24:17
   |
24 |         let a = &mut A as *mut usize;
   |                 ^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
   = note: this will be a hard error in the 2024 edition
   = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
   = note: `#[warn(static_mut_refs)]` on by default
help: use `addr_of_mut!` instead to create a raw pointer
   |
24 |         let a = addr_of_mut!(A) as *mut usize;
   |                 ~~~~~~~~~~~~~~~

warning: field `id` is never read
  --> src/bin/ch8b_stackful_coroutine.rs:36:5
   |
35 | struct Task {
   |        ---- field in this struct
36 |     id: usize,
   |     ^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `user_lib` (bin "ch8b_race_adder_atomic") generated 1 warning
warning: `user_lib` (bin "ch7b_pipe_large_test") generated 1 warning (run `cargo fix --bin "ch7b_pipe_large_test"` to apply 1 suggestion)
warning: `user_lib` (bin "ch8b_race_adder_mutex_spin") generated 1 warning
warning: `user_lib` (bin "ch8b_stackful_coroutine") generated 2 warnings
warning: `user_lib` (bin "ch8b_race_adder_loop") generated 1 warning
    Finished `release` profile [optimized] target(s) in 2.28s
make[1]: Leaving directory '/home/ezra/2024s-rcore-255doesnotexist/user'
Platform: qemu
   Compiling os v0.1.0 (/home/ezra/2024s-rcore-255doesnotexist/os)
   Compiling zero v0.1.3
   Compiling xmas-elf v0.7.0
    Finished `release` profile [optimized] target(s) in 1.25s
[rustsbi] RustSBI version 0.3.0-alpha.2, 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!
[kernel] back to world!
remap_test passed!
init TASK_MANAGER
num_app = 11
[kernel] PageFault in application, bad addr = 0x0, bad instruction = 0x3ac, kernel killed it.
[kernel] IllegalInstruction in application, kernel killed it.
[kernel] IllegalInstruction in application, kernel killed it.
Hello, world from user mode program!
power_3 [10000/200000]
power_3 [20000/200000]
power_3 [30000/200000]
power_3 [40000/200000]
power_3 [50000/200000]
power_5 [10000/140000]
power_5 [20000/140000]
power_5 [30000/140000]
power_5 [40000/140000]
power_5 [50000/140000]
power_5 [60000/140000]
power_5 [70000/140000]
power_5 [80000/140000]
power_5 [90000/140000]
power_5 [100000/140000]
power_5 [110000/140000]
power_5 [120000/140000]
power_5 [130000/140000]
power_5 [140000/140000]
5^140000 = 386471875(MOD 998244353)
Test power_5 OK!
power_7 [10000/160000]
power_7 [20000/160000]
power_7 [30000/160000]
power_7 [40000/160000]
AAAAAAAAAA [1/5]
BBBBBBBBBB [1/5]
CCCCCCCCCC [1/5]
Test sbrk start.
origin break point = c000
one page allocated,  break point = d000
try write to allocated page
write ok
10 page allocated,  break point = 17000
11 page DEALLOCATED,  break point = c000
try DEALLOCATED more one page, should be failed.
Test sbrk almost OK!
now write to deallocated page, should cause page fault.
[kernel] PageFault in application, bad addr = 0xc000, bad instruction = 0x5c8, kernel killed it.
power_3 [60000/200000]
power_3 [70000/200000]
power_3 [80000/200000]
power_3 [90000/200000]
power_7 [50000/160000]
power_7 [60000/160000]
power_7 [70000/160000]
power_7 [80000/160000]
power_7 [90000/160000]
power_7 [100000/160000]
power_7 [110000/160000]
power_7 [120000/160000]
power_7 [130000/160000]
power_7 [140000/160000]
power_7 [150000/160000]
power_7 [160000/160000]
7^160000 = 667897727(MOD 998244353)
Test power_7 OK!
AAAAAAAAAA [2/5]
BBBBBBBBBB [2/5]
CCCCCCCCCC [2/5]
power_3 [100000/200000]
power_3 [110000/200000]
power_3 [120000/200000]
power_3 [130000/200000]
power_3 [140000/200000]
power_3 [150000/200000]
power_3 [160000/200000]
power_3 [170000/200000]
power_3 [180000/200000]
AAAAAAAAAA [3/5]
BBBBBBBBBB [3/5]
CCCCCCCCCC [3/5]
power_3 [190000/200000]
power_3 [200000/200000]
3^200000 = 871008973(MOD 998244353)
Test power_3 OK!
AAAAAAAAAA [4/5]
BBBBBBBBBB [4/5]
CCCCCCCCCC [4/5]
AAAAAAAAAA [5/5]
BBBBBBBBBB [5/5]
CCCCCCCCCC [5/5]
Test write A OK!
Test write B OK!
Test write C OK!
[kernel] Panicked at src/task/mod.rs:153 All applications completed!