os/
main.rs

1//! ComixOS - A RISC-V operating system kernel
2//!
3//! This is the main crate for ComixOS, an operating system kernel written in Rust
4//! for RISC-V architecture. It provides basic OS functionalities including memory
5//! management, process scheduling, and system call handling.
6
7#![no_std]
8#![no_main]
9#![feature(custom_test_frameworks)]
10#![test_runner(test_runner)]
11#![reexport_test_harness_main = "test_main"]
12
13extern crate alloc;
14
15mod arch;
16mod config;
17mod device;
18mod fs;
19mod ipc;
20mod kernel;
21mod mm;
22mod security;
23mod sync;
24mod test;
25mod uapi;
26mod util;
27mod vfs;
28#[macro_use]
29mod log;
30mod net;
31
32use crate::arch::lib::sbi::shutdown;
33use core::arch::{asm, global_asm};
34use core::panic::PanicInfo;
35#[cfg(test)]
36use test::test_runner;
37#[cfg(target_arch = "riscv64")]
38global_asm!(include_str!("./arch/riscv/boot/entry.S"));
39
40/// Rust 内核主入口点
41///
42/// 这是从汇编代码跳转到的第一个 Rust 函数。它负责初始化内核的所有子系统,
43/// 包括内存管理、中断处理、定时器和任务调度器。
44///
45/// # Safety
46///
47/// 此函数标记为 `#[unsafe(no_mangle)]` 以确保链接器可以找到它。
48/// 它必须从正确初始化的汇编入口点调用。
49#[unsafe(no_mangle)]
50pub extern "C" fn rust_main(_hartid: usize) -> ! {
51    arch::boot::main(_hartid);
52    unreachable!("Unreachable in rust_main()");
53}
54
55#[panic_handler]
56fn panic(info: &PanicInfo) -> ! {
57    if let Some(location) = info.location() {
58        earlyprintln!(
59            "Panicked at {}:{} {}",
60            location.file(),
61            location.line(),
62            info.message()
63        );
64    } else {
65        earlyprintln!("Panicked: {}", info.message());
66    }
67
68    // LoongArch virt 没有 ACPI GED 关机路径,直接停机避免再次陷阱。
69    #[cfg(target_arch = "loongarch64")]
70    {
71        loop {
72            unsafe { asm!("idle 0") }
73        }
74    }
75
76    // 其他架构仍按原行为触发关机。
77    #[cfg(not(target_arch = "loongarch64"))]
78    shutdown(true)
79}