#![no_std] // 不链接 Rust 标准库 #![no_main] // 禁用 main 入口点,因为没有运行时 #![feature(custom_test_frameworks)] #![test_runner(anos::test_runner)] #![reexport_test_harness_main = "test_main"] use core::panic::PanicInfo; use anos::println; #[no_mangle] // 不重整函数名,否则编译器可能会生成名为 _ZN3blog_os4_start7hb173fedf945531caE 的函数 // extern "C" 告诉编译器这个函数应当使用 C 语言的调用约定 // 函数名为 _start 是因为大多数系统默认用这个名字作为入口点名称 // -> ! 代表这是一个发散函数,不允许有任何返回值,因为它不会被任何函数调用,而是将直接被 bootloader 调用 pub extern "C" fn _start() -> ! { println!("Hello World{}", "!"); anos::init(); // 触发一个中断 x86_64::instructions::interrupts::int3(); println!("It didn't crash!"); // 触发一个页错误 // 由于我们没有设定页错误的处理函数 // 所以会触发 double fault unsafe { *(0xdeadbeef as *mut u64) = 42; // 强制写入某个内存地址 } #[cfg(test)] test_main(); loop {} } // 在非测试模式下,将 panic 信息打印到 VGA 缓冲区 #[cfg(not(test))] #[panic_handler] // panic! 时将会进行栈展开,执行各种 drop 函数来回收垃圾 // 禁用标准库之后,这些东西就都没有了,我们需要把它重写一遍 fn panic(info: &PanicInfo) -> ! { println!("{}", info); loop {} } #[cfg(test)] #[panic_handler] fn panic(info: &PanicInfo) -> ! { anos::test_panic_handler(info) }