Raspberry Pi Bare Bones Rust:用 Rust 完成树莓派裸机开发
文章目录
|
本页面或章节使用 I, 我, 我们 或 我们来指代读者或编辑者。应编辑 应改写为百科全书式的语气。 |
|
等等!你是否已阅读 Getting Started, Beginner Mistakes,以及一些相关的 OS theory? |
| 难度级别 |
|---|
高级 |
本教程直接接续 Raspberry Pi Bare Bones ,使用 Rust 而非 C 完成相同的任务。
先决条件
以下说明假设你已具备所有工具,并已成功按照 Raspberry Pi Bare Bones 操作并在 QEMU 中运行了你的内核。同时假设你熟悉 Rust 和 Cargo。
我们将使用 rustup 来管理 Rust。
概述
在 C 教程中,你在 boot.S 中创建了对符号 "kernel_main" 的引用。
// Call kernel_main ldr r3, =kernel_main
在该教程的最后,编译后的 kernel.c 目标文件 kernel.o 在创建 kernel.elf 时,向链接器提供了该符号的定义,即其函数 "kernel_main"。
我们将改为创建一个定义符号 kernel_main 的 Rust 库(.rlib)。
设置
为简洁起见,我们假设环境中尚未安装 Rust。
curl https://sh.rustup.rs -sSf | sh
这将安装 rustup,它将管理 Rust 编译器和 Cargo。
rustup component add rust-src
我们稍后需要重新编译 Rust 语言的部分内容,因此需要此组件。
cargo install xargo
我们将使用 xargo 来构建项目,它可作为 Cargo 的直接替代品使用。xargo 会自动处理一项重要任务:为我们的目标编译并链接所需的 Rust 语言组件(例如 libcore.rs)。
xargo new kernel cd kernel rustup override set nightly
这将创建我们的 crate,并让 rustup 为其使用 nightly Rust 工具链。我们需要使用 stable 版本中不可用的 Rust 特性。
在该目录中创建一个名为 arm-none-eabihf.json 的文件,内容如下:
{ "llvm-target": "arm-none-eabihf", "target-endian": "little", "target-pointer-width": "32", "target-c-int-width": "32", "os": "none", "env": "eabi", "vendor": "unknown", "arch": "arm", "linker-flavor": "gcc", "linker": "arm-none-eabi-gcc", "data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64", "executables": true, "relocation-model": "static", "no-compiler-rt": true }
Rust 使用 LLVM 作为后端,对于非标准(没有 triple)的编译目标,它需要这种规范文件。填写其中的值超出了本教程的范围;更多信息可在 rust-cross 中找到,该文档详细介绍了 Rust 的交叉编译。
在你的 Cargo.toml 中添加
[profile.dev] panic = "abort"
这告诉 rustc 不要编译栈展开(stack unwinding)。这允许从 panic 中恢复,但当内核发生 panic 时,没有任何东西可以尝试恢复。
编辑 src/lib.rs 使其包含
#![no_std]
#[no_mangle]
pub extern fn kernel_main() {}
现在我们可以构建包含 kernel_main 定义的 rlib 了:
xargo build --target arm-none-eabihf
.
现在,重新执行 C 教程中构建 kernel.elf 的命令,但不要使用 kernel.o,而是提供 xargo 刚刚在 target 目录中构建的 libkernel.rlib。
arm-none-eabi-gcc -T linker.ld -o myos.elf -ffreestanding -O2 -nostdlib boot.o path/to/libkernel.rlib
就是这样!你已经为可在 Raspberry Pi 上运行的裸机 arm 构建了 Rust 代码!
Hello
C 教程使用 UART 来输出 hello。下面的代码用 Rust 实现了同样的功能。它不像 C 代码那样配置 UART,因为 QEMU 的 raspi2 机器具有可工作的默认状态。要在真实硬件上工作,请注意根据你的使用场景配置 UART。你可以在 bcm2835 manual中了解相关内容。将以下内容放入 src/lib.rs 并重新构建你的 ELF:
#![no_std]
#![feature(core_intrinsics, lang_items)]
use core::intrinsics::abort;
use core::intrinsics::volatile_load;
use core::intrinsics::volatile_store;
use core::panic::PanicInfo;
// raspi2 and raspi3 have peripheral base address 0x3F000000,
// but raspi1 has peripheral base address 0x20000000. Ensure
// you are using the correct peripheral address for your
// hardware.
const UART_DR: u32 = 0x3F201000;
const UART_FR: u32 = 0x3F201018;
fn mmio_write(reg: u32, val: u32) {
unsafe { volatile_store(reg as *mut u32, val) }
}
fn mmio_read(reg: u32) -> u32 {
unsafe { volatile_load(reg as *const u32) }
}
fn transmit_fifo_full() -> bool {
mmio_read(UART_FR) & (1 << 5) > 0
}
fn receive_fifo_empty() -> bool {
mmio_read(UART_FR) & (1 << 4) > 0
}
fn writec(c: u8) {
while transmit_fifo_full() {}
mmio_write(UART_DR, c as u32);
}
fn getc() -> u8 {
while receive_fifo_empty() {}
mmio_read(UART_DR) as u8
}
fn write(msg: &str) {
for c in msg.chars() {
writec(c as u8)
}
}
#[no_mangle]
pub extern fn kernel_main() {
write("Hello Rust Kernel world!");
loop {
writec(getc())
}
}
// These functions below provide definitions for symbols libcore
// expects which are not present on our bare metal target. You
// will not encounter linker errors until you use a part of
// libcore that references them, such as iterators in this program.
// In the future you may need to provide real implementations for
// these functions.
#[no_mangle]
pub extern fn __aeabi_unwind_cpp_pr0() {}
#[lang = "eh_personality"]
pub extern fn eh_personality() {}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
unsafe { abort() }
}
#[allow(non_snake_case)]
#[no_mangle]
pub extern fn _Unwind_Resume() { loop {} }
如果你运行它,你会看到你的文本打印在虚拟串口上:
qemu-system-arm -M raspi2 -kernel myos.elf -serial stdio
外部资源
- Rust programming language
- rustup, Rust components manager
- rust-raspi3-tutorials 将 bzt 的 C tutorials 用 Rust 语言重写
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Raspberry Pi Bare Bones Rust;离线修订号:28447。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论