strat9_kernel/components.rs
1//! Kernel component declarations : Strat9-OS boot orchestration.
2//!
3//! Each function annotated with `#[component::init_component]` is registered
4//! in the `.component_entries` linker section. `component::init_all()` reads
5//! that section at runtime, topologically sorts the entries according to their
6//! `depends_on` edges (with `priority` as a tiebreaker), and calls them in order.
7//!
8//! ## Stages
9//!
10//! | Stage | When |
11//! |-------------|-----------------------------------------|
12//! | `bootstrap` | Before SMP, early kernel init |
13//! | `kthread` | After SMP, in kernel-thread context |
14//! | `hardware` | After scheduler, device/driver probing |
15//! | `process` | After first user process is created |
16//!
17//! ## Syntax
18//!
19//! ```rust,no_run
20//! #[component::init_component(bootstrap, priority = 1)]
21//! fn vfs_init() -> Result<(), ComponentInitError> { … }
22//!
23//! #[component::init_component(kthread, priority = 2, depends_on = vfs_init)]
24//! fn fs_ext4_init() -> Result<(), ComponentInitError> { … }
25//!
26//! #[component::init_component(hardware, priority = 1)]
27//! fn drivers_init() -> Result<(), ComponentInitError> { … }
28//!
29//! #[component::init_component(kthread, priority = 3, depends_on = [vfs_init, ipc_init])]
30//! fn silo_init() -> Result<(), ComponentInitError> { … }
31//! ```
32
33use component::ComponentInitError;
34
35// ============================================================================
36// Bootstrap stage : early kernel init (before SMP)
37//
38// Components that depend on local kernel_main state (e.g. mmap_work for
39// memory_init) remain as markers : the real init happens inline in kernel_main
40// before `init_all(Bootstrap)` is called. Components that are self-contained
41// do their real work here.
42// ============================================================================
43
44/// Memory management : marker : real init is inline in kernel_main (Phase 2)
45/// because it requires the working memory map which is a local variable.
46#[component::init_component(bootstrap, priority = 0)]
47fn memory_init() -> Result<(), ComponentInitError> {
48 log::info!("[component] Memory management initialized");
49 Ok(())
50}
51
52/// Logger : marker : already initialized before the component system runs.
53#[component::init_component(bootstrap, priority = 1, depends_on = memory_init)]
54fn logger_init() -> Result<(), ComponentInitError> {
55 log::info!("[component] Logger initialized");
56 Ok(())
57}
58
59/// Architecture primitives (TSS, GDT, SYSCALL) : needs memory for TSS allocation.
60/// Marker : real init is inline in kernel_main because it must run before
61/// the component system is invoked (bootstrap components depend on GDT/TSS).
62#[component::init_component(bootstrap, priority = 1, depends_on = memory_init)]
63fn arch_init() -> Result<(), ComponentInitError> {
64 log::info!("[component] Architecture primitives initialized (marker)");
65 Ok(())
66}
67
68/// Synchronization primitives : foundational, no deps beyond memory.
69#[component::init_component(bootstrap, priority = 2, depends_on = memory_init)]
70fn sync_init() -> Result<(), ComponentInitError> {
71 log::info!("[component] Sync primitives initialized");
72 Ok(())
73}
74
75/// ACPI and power management.
76#[component::init_component(bootstrap, priority = 3, depends_on = [memory_init, arch_init])]
77fn acpi_init() -> Result<(), ComponentInitError> {
78 log::info!("[component] ACPI initialized");
79 Ok(())
80}
81
82/// Capability-based security : needs memory; used by VFS and IPC.
83#[component::init_component(bootstrap, priority = 3, depends_on = memory_init)]
84fn capability_init() -> Result<(), ComponentInitError> {
85 // Capability system is initialized on first use (spin::Once).
86 // This marker ensures the component graph reflects the dependency.
87 log::info!("[component] Capability system initialized");
88 Ok(())
89}
90
91/// Virtual file system : needs memory and capability subsystem.
92/// Marker : real init is inline in kernel_main because it requires
93/// `register_boot_modules()` with boot args that aren't available here.
94#[component::init_component(bootstrap, priority = 4, depends_on = [memory_init, capability_init])]
95fn vfs_init() -> Result<(), ComponentInitError> {
96 log::info!("[component] VFS initialized (marker)");
97 Ok(())
98}
99
100/// IPC : inter-process communication primitives.
101#[component::init_component(bootstrap, priority = 4, depends_on = [memory_init, capability_init])]
102fn ipc_init() -> Result<(), ComponentInitError> {
103 log::info!("[component] IPC initialized");
104 Ok(())
105}
106
107/// Driver framework : needs arch primitives and memory.
108#[component::init_component(bootstrap, priority = 5, depends_on = [memory_init, arch_init])]
109fn drivers_init() -> Result<(), ComponentInitError> {
110 log::info!("[component] Driver framework initialized");
111 Ok(())
112}
113
114// ============================================================================
115// Kthread stage : after SMP, in kernel-thread context
116// ============================================================================
117
118/// Process and task management : marker : scheduler is already running
119/// (init_all(Kthread) is called after init_scheduler() in kernel_main).
120#[component::init_component(kthread, priority = 0)]
121fn process_init() -> Result<(), ComponentInitError> {
122 log::info!("[component] Process management initialized");
123 Ok(())
124}
125
126/// Namespace management : depends on VFS being ready (bootstrap stage).
127/// Cross-stage dep on `vfs_init` is skipped by the topo-sort (it's in a
128/// different stage) and is guaranteed by stage ordering.
129#[component::init_component(kthread, priority = 1, depends_on = process_init)]
130fn namespace_init() -> Result<(), ComponentInitError> {
131 log::info!("[component] Namespace management initialized");
132 Ok(())
133}
134
135/// Syscall interface : needs process + arch.
136#[component::init_component(kthread, priority = 1, depends_on = process_init)]
137fn syscall_init() -> Result<(), ComponentInitError> {
138 log::info!("[component] Syscall interface initialized");
139 Ok(())
140}
141
142/// Silo management : needs process + namespace + syscall.
143#[component::init_component(kthread, priority = 2, depends_on = [namespace_init, syscall_init])]
144fn silo_init() -> Result<(), ComponentInitError> {
145 log::info!("[component] Silo management initialized");
146 Ok(())
147}
148
149// ============================================================================
150// Hardware stage : after scheduler, device/driver probing
151// ============================================================================
152
153/// PCI bus enumeration and early hardware probing.
154#[component::init_component(hardware, priority = 0)]
155fn pci_init() -> Result<(), ComponentInitError> {
156 crate::hardware::init();
157 log::info!("[component] PCI and hardware framework initialized");
158 Ok(())
159}
160
161/// Storage drivers (VirtIO block, AHCI, NVMe).
162#[component::init_component(hardware, priority = 1, depends_on = pci_init)]
163fn storage_init() -> Result<(), ComponentInitError> {
164 crate::hardware::storage::virtio_block::init();
165 crate::hardware::storage::ahci::init();
166 crate::hardware::storage::nvme::init();
167 log::info!("[component] Storage drivers initialized");
168 Ok(())
169}
170
171/// Network drivers (VirtIO net, E1000).
172///
173/// Real probe already ran in `pci_init` → `hardware::init()` → `nic::init()`.
174/// This stage only confirms the registry is populated.
175#[component::init_component(hardware, priority = 1, depends_on = pci_init)]
176fn nic_init() -> Result<(), ComponentInitError> {
177 if crate::hardware::nic::list_interfaces().is_empty() {
178 crate::hardware::nic::virtio_net::init();
179 }
180 log::info!("[component] Network drivers initialized");
181 Ok(())
182}
183
184/// Timer subsystem (HPET, RTC, APIC timer start).
185#[component::init_component(hardware, priority = 2, depends_on = pci_init)]
186fn timer_init() -> Result<(), ComponentInitError> {
187 crate::hardware::timer::init();
188 log::info!("[component] Timer subsystem initialized");
189 Ok(())
190}
191
192/// USB controllers (xHCI, EHCI, UHCI).
193#[component::init_component(hardware, priority = 2, depends_on = pci_init)]
194fn usb_init() -> Result<(), ComponentInitError> {
195 crate::hardware::usb::init();
196 log::info!("[component] USB controllers initialized");
197 Ok(())
198}
199
200// ============================================================================
201// Process stage : after the first user process has been created
202// ============================================================================
203
204/// Network stack (userspace component stub).
205#[component::init_component(process, priority = 0)]
206fn network_init() -> Result<(), ComponentInitError> {
207 log::info!("[component] Network stack initialized");
208 Ok(())
209}
210
211/// Filesystem servers (userspace components).
212#[component::init_component(process, priority = 1, depends_on = network_init)]
213fn filesystem_init() -> Result<(), ComponentInitError> {
214 log::info!("[component] Filesystem servers initialized");
215 Ok(())
216}