Skip to main content

os/
main.rs

1//! The main module and entrypoint
2//!
3//! The operating system and app also starts in this module. Kernel code starts
4//! executing from `entry.asm`, after which [`rust_main()`] is called to
5//! initialize various pieces of functionality [`clear_bss()`]. (See its source code for
6//! details.)
7//!
8//! We then call [`println!`] to display `Hello, world!`.
9
10#![deny(missing_docs)]
11#![deny(warnings)]
12#![no_std]
13#![no_main]
14
15macro_rules! linker_symbol_addr {
16    ($symbol:path) => {
17        ($symbol as *const ()).addr()
18    };
19}
20
21use core::arch::global_asm;
22use log::*;
23
24#[macro_use]
25mod console;
26mod lang_items;
27mod logging;
28mod sbi;
29
30global_asm!(include_str!("entry.asm"));
31
32/// clear BSS segment
33pub fn clear_bss() {
34    unsafe extern "C" {
35        safe fn sbss();
36        safe fn ebss();
37    }
38    (linker_symbol_addr!(sbss)..linker_symbol_addr!(ebss))
39        .for_each(|a| unsafe { (a as *mut u8).write_volatile(0) });
40}
41
42/// the rust entry-point of os
43#[unsafe(no_mangle)]
44pub fn rust_main() -> ! {
45    unsafe extern "C" {
46        safe fn stext(); // begin addr of text segment
47        safe fn etext(); // end addr of text segment
48        safe fn srodata(); // start addr of Read-Only data segment
49        safe fn erodata(); // end addr of Read-Only data ssegment
50        safe fn sdata(); // start addr of data segment
51        safe fn edata(); // end addr of data segment
52        safe fn sbss(); // start addr of BSS segment
53        safe fn ebss(); // end addr of BSS segment
54        safe fn boot_stack_lower_bound(); // stack lower bound
55        safe fn boot_stack_top(); // stack top
56    }
57    clear_bss();
58    logging::init();
59    println!("[kernel] Hello, world!");
60    trace!(
61        "[kernel] .text [{:#x}, {:#x})",
62        linker_symbol_addr!(stext),
63        linker_symbol_addr!(etext)
64    );
65    debug!(
66        "[kernel] .rodata [{:#x}, {:#x})",
67        linker_symbol_addr!(srodata),
68        linker_symbol_addr!(erodata)
69    );
70    info!(
71        "[kernel] .data [{:#x}, {:#x})",
72        linker_symbol_addr!(sdata),
73        linker_symbol_addr!(edata)
74    );
75    warn!(
76        "[kernel] boot_stack top=bottom={:#x}, lower_bound={:#x}",
77        linker_symbol_addr!(boot_stack_top),
78        linker_symbol_addr!(boot_stack_lower_bound)
79    );
80    error!(
81        "[kernel] .bss [{:#x}, {:#x})",
82        linker_symbol_addr!(sbss),
83        linker_symbol_addr!(ebss)
84    );
85
86    // CI autotest success: sbi::shutdown(false)
87    // CI autotest failed : sbi::shutdown(true)
88    sbi::shutdown(false)
89}