这是 os summer of code 2020 项目每日记录的一部分:
每日记录github地址(包含根据实验指导实现的每个阶段的代码):https://github.com/yunwei37/os-summer-of-code-daily
这里参考的是rCore tutorial的第三版:https://github.com/rcore-os/rCore-Tutorial
rCore tutorial environment && lab 0
环境配置
本机环境(新安装的虚拟机系统,上一个系统是搞xv6的qemu,感觉可能实验环境会有冲突不如再开一个):
Linux ubuntu 5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
已换源至阿里云,但版本过低;
安装 QEMU
- 先
sudo apt-get install build-essential
安装编译工具链; - 下载源码编译:
wget https://download.qemu.org/qemu-4.2.0.tar.xz
tar xvJf qemu-4.2.0.tar.xz
cd qemu-4.2.0
./configure --target-list=riscv32-softmmu,riscv64-softmmu
- 报错
ERROR: glib-2.48 gthread-2.0 is required to compile QEMU
:通过sudo apt-get install libglib2.0-dev
安装; - 报错
ERROR: pixman >= 0.21.8 not present
,通过sudo apt-get install libpixman-1-dev
安装 - 测试
yunwei@ubuntu:~/rcore$ qemu-system-riscv64 --version
QEMU emulator version 4.2.0
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
安装 Rust 工具链
- 运行
curl https://sh.rustup.rs -sSf | sh
;
尝试运行 rCore
git clone https://github.com/rcore-os/rCore-Tutorial
make
Failed to execute tool: objcopy
No such file or directory (os error 2)
make[1]: *** [Makefile:28: target/riscv64imac-unknown-none-elf/debug/kernel.bin] Error 101
可能是我先做了下面导致的问题,之后再说;
lab 0: 跑起来
创建 Rust 项目
创建文件夹,并创建项目:
mkdir Project
cd Project
echo "nightly-2020-06-27" > rust-toolchain
cargo new os
cd os
- Test:
cargo run
移除标准库依赖:
main.rs
#![no_std]
use core::panic::PanicInfo;
/// 当 panic 发生时会调用该函数
/// 我们暂时将它的实现为一个死循环
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
Cargo.toml
[package]
name = "os"
version = "0.1.0"
authors = ["yunwei"]
edition = "2018"
# panic 时直接终止,因为我们没有实现堆栈展开的功能
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
移除运行时环境依赖
main.rs
#![no_std]
use core::panic::PanicInfo;
/// 当 panic 发生时会调用该函数
/// 我们暂时将它的实现为一个死循环
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
- 运行:
rustup target add riscv64imac-unknown-none-elf
- 运行:
cargo build --target riscv64imac-unknown-none-elf
结果:
yunwei@ubuntu:~/Project/os$ cargo build --target riscv64imac-unknown-none-elf
Compiling os v0.1.0 (/home/yunwei/Project/os)
Finished dev [unoptimized + debuginfo] target(s) in 0.06s
在 os/.cargo/config 里面添加了这个:
# 编译的目标平台
[build]
target = "riscv64imac-unknown-none-elf"
就可以 cargo build 啦
生成内核镜像
运行:
cargo install cargo-binutils
rustup component add llvm-tools-preview
rust-objdump --version
@ubuntu:~/Project/os$ rust-objdump --version
LLVM (http://llvm.org/):
LLVM version 10.0.1-rust-1.46.0-nightly
Optimized build.
Default target: x86_64-unknown-linux-gnu
Host CPU: skylake
Registered Targets:
aarch64 - AArch64 (little endian)
aarch64_32 - AArch64 (little endian ILP32)
aarch64_be - AArch64 (big endian)