#![no_std] // don't link the Rust standard library
#![no_main] // disable all Rust-level entry points
use core::panic::PanicInfo;
#[no_mangle] // don't mangle the name of this function
pub extern "C" fn _start() -> ! {
// this function is the entry point, since the linker looks for a function
// named `_start` by default
loop {}
}
/// This function is called on panic.
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
无聊
A Freestanding Rust Binary
#![no_std]
ban掉与操作系统相关的线程,文件,网络等特性。与C的标准libc
有关的都不要。写操作系统怎么能依赖于别的操作系统panic
是依赖于标准库的,但是rust需要#[panic_handler]
,这里自定义了个panic
,需要core::panic::PanicInfo
,PanicInfo
包含了文件的行号和文件,且函数是无返回值的eh_personality
和copy
一样是编译器内部需要使用的,虽然可以自定义但是非常不稳定不安全。这里采用panic = "abort"
来中断eh_personality
属性提供的unwinding
没
main
入口点,缺少start
属性。使用#![no_main]
自定义入口点#![no_mangle]
ban掉name manging
,这个和编译器识别函数有关了extern 'C'
使用C语言的约定调用而不是rust的。rust的约定调用是啥?_start
是大部分系统默认的入口点,且不能被其他函数调用,只能被boot板唤起链接器(linker)默认程序依赖于C runtime。显然我们没用,所以报错。我们需要传入一些参数告诉linker我们不需要runtime
注释很清楚了
最小化内核
nightly
版本rust支持#![feature(asm)]
写汇编core
包包含基础的 Rust 类型,如Result
、Option
和迭代器等;compiler_builtins
包提供 LLVM 需要的许多底层操作,比如memcpy
。core
库以预编译库(precompiled library)的形式与 Rust 编译器一同发布——这时,core
库只对支持的宿主系统有效,而我们自定义的目标系统无效core
库和一些编译器内建库(compiler built-in libraries).cargo/config
的cargo配置文件自动添加--target
参数bootimage
解决cargo run
更简单的运行qemuVGA 字符模式
repr(transparent)
内核测试
#[test_case]
自定义测试框架,缺点是有一些高级功能诸如should_panic
tests都不能用,需要自己来实现。