Skip to main content

somehal_macros/
lib.rs

1use proc_macro::TokenStream;
2
3mod _entry;
4mod trap;
5
6/// Attribute to declare the entry point of the program
7///
8/// **IMPORTANT**: This attribute must appear exactly *once* in the dependency graph. Also, if you
9/// are using Rust 1.30 the attribute must be used on a reachable item (i.e. there must be no
10/// private modules between the item and the root of the crate); if the item is in the root of the
11/// crate you'll be fine. This reachability restriction doesn't apply to Rust 1.31 and newer releases.
12///
13/// The specified function will be called by the reset handler *after* RAM has been initialized.
14/// If present, the FPU will also be enabled before the function is called.
15///
16/// The type of the specified function must be `[unsafe] fn() -> !` (never ending function)
17///
18/// # 属性参数
19///
20/// 此宏**必需**接受一个内核类型参数:
21///
22/// - **参数**: 指定实现 `somehal::KernelOp` trait 的类型标识符
23/// - **行为**: 自动在函数开头插入 `somehal::init(&<type>)` 调用
24///
25/// # Properties
26///
27/// The entry point will be called by the reset handler. The program can't reference to the entry
28/// point, much less invoke it.
29///
30/// # Examples
31///
32/// - Entry point with kernel initialization
33///
34/// ``` no_run
35/// # #![no_main]
36/// # use pie_boot::entry;
37/// # struct Kernel;
38/// # impl somehal::KernelOp for Kernel {
39/// #     fn ioremap(&self, paddr: usize, size: usize) -> somehal::PagingResult<*mut u8> {
40/// #         Ok(std::ptr::null_mut())
41/// #     }
42/// # }
43/// #[entry(Kernel)]
44/// fn main() -> ! {
45///     // somehal::init(&Kernel) 已自动生成
46///     loop { /* .. */ }
47/// }
48/// ```
49#[proc_macro_attribute]
50pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
51    _entry::entry(args, input, "__someboot_main")
52}
53
54/// Attribute to declare the secondary entry point of the program
55///
56/// # Examples
57///
58/// - Simple entry point
59///
60/// ``` no_run
61/// # #![no_main]
62/// # use pie_boot::secondary_entry;
63/// #[entry]
64/// fn secondary(cpu_id: usize) -> ! {
65///     loop { /* .. */ }
66/// }
67/// ```
68#[proc_macro_attribute]
69pub fn someboot_secondary_entry(args: TokenStream, input: TokenStream) -> TokenStream {
70    _entry::entry_secondary(args, input, true)
71}
72
73/// Attribute to declare the secondary entry point of the program
74///
75/// # Examples
76///
77/// - Simple entry point
78///
79/// ``` no_run
80/// # #![no_main]
81/// # use pie_boot::secondary_entry;
82/// #[entry]
83/// fn secondary(cpu_id: usize) -> ! {
84///     loop { /* .. */ }
85/// }
86/// ```
87#[proc_macro_attribute]
88pub fn somehal_secondary_entry(args: TokenStream, input: TokenStream) -> TokenStream {
89    _entry::entry_secondary(args, input, false)
90}
91
92/// 中断处理器属性宏
93///
94/// 将用户函数转换为标准中断处理器,自动生成正确的函数签名。
95///
96/// # 要求
97///
98/// 函数必须:
99/// - 不能是 `const`、`async` 或有泛型参数
100/// - 必须有且仅有一个参数,类型为 `someboot::irq::IrqId`
101/// - 无返回类型
102/// - 不应有显式的可见性声明(宏自动设为 `pub`)
103///
104/// # 示例
105///
106/// ```
107/// # #![no_std]
108/// # use somehal_macros::irq_handler;
109/// # use someboot::irq::IrqId;
110/// #[irq_handler]
111/// fn my_irq_handler(irq: IrqId) {
112///     // 处理中断
113///     sparreal_kernel::os::irq::handle_irq(irq);
114/// }
115/// ```
116///
117/// # 生成代码
118///
119/// 宏展开为:
120///
121/// ```ignore
122/// #[unsafe(no_mangle)]
123/// pub extern "Rust" fn _someboot_handle_irq(irq: someboot::irq::IrqId) {
124///     // 你的代码
125/// }
126/// ```
127///
128/// # 平台集成
129///
130/// 该函数由 HAL 的 trap 处理代码自动调用:
131///
132/// - AArch64: `crates/somehal/src/arch/aarch64/trap.rs`
133/// - LoongArch64: `crates/somehal/src/arch/loongarch64/trap.rs`
134///
135/// 中断号随后通过 `sparreal_kernel::os::irq::handle_irq()` 分发到注册的处理函数。
136///
137/// # 注意
138///
139/// - 每个平台只能有一个全局中断入口(符号名固定为 `_someboot_handle_irq`)
140/// - 参数名会被保留(如 `irq`、`hwirq` 等),不会被强制修改
141#[proc_macro_attribute]
142pub fn irq_handler(args: TokenStream, input: TokenStream) -> TokenStream {
143    trap::irq_handler(args, input)
144}