forked from YdrMaster/rCore-Tutorial-in-single-workspace
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
67 lines (57 loc) · 1.6 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#![no_std]
#![no_main]
#![feature(naked_functions, asm_const)]
#![deny(warnings)]
#[macro_use]
extern crate rcore_console;
use sbi_rt::*;
/// Supervisor 汇编入口。
///
/// 设置栈并跳转到 Rust。
#[naked]
#[no_mangle]
#[link_section = ".text.entry"]
unsafe extern "C" fn _start() -> ! {
const STACK_SIZE: usize = 4096;
#[link_section = ".bss.uninit"]
static mut STACK: [u8; STACK_SIZE] = [0u8; STACK_SIZE];
core::arch::asm!(
"la sp, {stack} + {stack_size}",
"j {main}",
stack_size = const STACK_SIZE,
stack = sym STACK,
main = sym rust_main,
options(noreturn),
)
}
/// 使用 `console` 输出的 Supervisor 裸机程序。
///
/// 测试各种日志和输出后关机。
extern "C" fn rust_main() -> ! {
// 初始化 `console`
rcore_console::init_console(&Console);
// 设置日志级别
rcore_console::set_log_level(option_env!("LOG"));
// 测试各种打印
rcore_console::test_log();
system_reset(Shutdown, NoReason);
unreachable!()
}
/// 将传给 `console` 的控制台对象。
///
/// 这是一个 Unit struct,它不需要空间。否则需要传一个 static 对象。
struct Console;
/// 为 `Console` 实现 `console::Console` trait。
impl rcore_console::Console for Console {
fn put_char(&self, c: u8) {
#[allow(deprecated)]
legacy::console_putchar(c as _);
}
}
/// Rust 异常处理函数,以异常方式关机。
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{info}");
system_reset(Shutdown, SystemFailure);
loop {}
}