Download kernel/src/main.rs from Jaswanth-2008/AeroOS: direct link, hf CLI and curl.
- Browser
- Download file 11.3 kB
-
https://huggingface.co/Jaswanth-2008/AeroOS/resolve/main/kernel/src/main.rs
- Command line
-
hf download hf://Jaswanth-2008/AeroOS/kernel/src/main.rs
-
curl -L -o main.rs https://huggingface.co/Jaswanth-2008/AeroOS/resolve/main/kernel/src/main.rs
11.3 kB
| extern crate alloc; | |
| pub mod arch; | |
| pub mod memory; | |
| pub mod drivers; | |
| pub mod graphics; | |
| pub mod fs; | |
| pub mod net; | |
| pub mod window_manager; | |
| pub mod desktop; | |
| pub mod apps; | |
| use core::panic::PanicInfo; | |
| use bootloader_api::{entry_point, BootInfo, BootloaderConfig}; | |
| use crate::apps::AppType; | |
| use crate::desktop::Desktop; | |
| use crate::drivers::framebuffer::{self, FRAMEBUFFER}; | |
| use crate::drivers::ps2_mouse; | |
| use crate::graphics::surface::Surface; | |
| use crate::window_manager::compositor::Compositor; | |
| pub static BOOTLOADER_CONFIG: BootloaderConfig = { | |
| let mut config = BootloaderConfig::new_default(); | |
| config.mappings.physical_memory = Some(bootloader_api::config::Mapping::Dynamic); | |
| config.kernel_stack_size = 1024 * 1024; // 1 MiB stack | |
| config | |
| }; | |
| entry_point!(kernel_main, config = &BOOTLOADER_CONFIG); | |
| fn kernel_main(boot_info: &'static mut BootInfo) -> ! { | |
| // 1. Initialize Serial Port Logger (COM1 0x3F8) | |
| drivers::serial::init(); | |
| println!("=================================================="); | |
| println!(" AeroOS Desktop Kernel v1.0.0 (x86_64) "); | |
| println!(" Windows 11 & macOS Hybrid Graphical Interface "); | |
| println!("=================================================="); | |
| // 2. Initialize GDT, IDT, PIC, and CPU Interrupts | |
| arch::init(); | |
| let t0 = arch::boot_timer::read_tsc(); | |
| println!("[+] Hardware Interrupts & GDT/IDT initialized."); | |
| drivers::speaker::play_startup_melody(); | |
| println!("[+] PC Speaker audio initialized (Chimes played)."); | |
| // 3. Initialize Memory Management & 16 MiB Heap Allocator | |
| let phys_mem_offset = boot_info | |
| .physical_memory_offset | |
| .into_option() | |
| .expect("Bootloader did not map physical memory"); | |
| memory::init(phys_mem_offset, &boot_info.memory_regions); | |
| println!("[+] 4-Level Paging & 16 MiB Heap Allocator initialized."); | |
| // 4. Initialize Virtual File System (RamDisk VFS) | |
| fs::init(); | |
| println!("[+] Virtual File System initialized with default documents."); | |
| // 5. Initialize Keyboard Subsystem | |
| drivers::ps2_keyboard::init(); | |
| println!("[+] PS/2 Keyboard driver active."); | |
| // 6. Initialize Framebuffer & Surface | |
| let fb = boot_info.framebuffer.as_mut().expect("No framebuffer available"); | |
| framebuffer::init(fb); | |
| let (screen_w, screen_h) = { | |
| let guard = FRAMEBUFFER.lock(); | |
| let cfg = guard.as_ref().unwrap(); | |
| (cfg.width, cfg.height) | |
| }; | |
| println!("[+] UEFI GOP Framebuffer: {}x{}", screen_w, screen_h); | |
| // 7. Initialize Mouse Driver | |
| drivers::ps2_mouse::init(screen_w as i32, screen_h as i32); | |
| println!("[+] PS/2 Mouse driver initialized."); | |
| // 8. Initialize CMOS Real-Time Clock | |
| let rtc_time = drivers::rtc::get_datetime(); | |
| println!("[+] Hardware CMOS RTC Initialized: {} ({})", rtc_time.format_full(), rtc_time.format_time_12()); | |
| arch::boot_timer::record_stage("1. Base Kernel & Core Subsystems", t0); | |
| // Component 1: Network connectivity (WiFi & LAN) | |
| let t_net = arch::boot_timer::read_tsc(); | |
| drivers::virtio_net::init(); | |
| net::init(); | |
| net::wifi::init(); | |
| println!("[+] Network connectivity active: VirtIO-Net (LAN) + 802.11ac (WiFi)"); | |
| arch::boot_timer::record_stage("2. Network Connectivity (WiFi & LAN)", t_net); | |
| // Component 2: Laptop Hardware Compatibility (ACPI, Backlight, Thermal, Power Profiles) | |
| let t_laptop = arch::boot_timer::read_tsc(); | |
| drivers::laptop::init(); | |
| println!("[+] Laptop Hardware Compatibility active (ACPI battery, backlight, power profiles, sensors)"); | |
| arch::boot_timer::record_stage("3. Laptop Hardware Compatibility", t_laptop); | |
| // Component 3: In-built package manager: APTITUDE alias APT (For debian) | |
| let t_apt = arch::boot_timer::read_tsc(); | |
| apps::apt::init(); | |
| println!("[+] Debian APT/Aptitude package manager engine initialized."); | |
| arch::boot_timer::record_stage("4. Package Manager (APT / Aptitude)", t_apt); | |
| // Component 4: In-built Store: snapstore | |
| let t_snap = arch::boot_timer::read_tsc(); | |
| apps::snapstore::init(); | |
| println!("[+] Canonical Snap Store subsystem & snapd daemon initialized."); | |
| arch::boot_timer::record_stage("5. In-built Store (Snap Store)", t_snap); | |
| // Component 5: Desktop Compositor & GUI Shell | |
| let t_gui = arch::boot_timer::read_tsc(); | |
| let mut surface = Surface::new(screen_w, screen_h); | |
| let mut desktop_surface = Surface::new(screen_w, screen_h); | |
| let mut compositor = Compositor::new(); | |
| let mut desktop = Desktop::new(); | |
| // Open initial desktop windows: Snap Store (right side) and Terminal (left side) | |
| compositor.open_window( | |
| "Snap Store - Software Center", | |
| 405, | |
| 25, | |
| 380, | |
| 515, | |
| AppType::SnapStore, | |
| ); | |
| compositor.open_window( | |
| "AeroShell - Terminal", | |
| 15, | |
| 25, | |
| 380, | |
| 515, | |
| AppType::Terminal, | |
| ); | |
| arch::boot_timer::record_stage("6. Desktop Compositor (Fluent UI)", t_gui); | |
| arch::boot_timer::print_boot_summary(); | |
| println!("[+] Windows 11 Fluent Desktop Compositor starting interactive event loop..."); | |
| let mut last_left_button = false; | |
| let mut last_mouse_x = screen_w as i32 / 2; | |
| let mut last_mouse_y = screen_h as i32 / 2; | |
| let mut last_ticks = 0; | |
| // Initial desktop render | |
| desktop.render(&mut desktop_surface, &mut compositor); | |
| surface.copy_from(&desktop_surface); | |
| surface.draw_mouse_cursor(last_mouse_x, last_mouse_y); | |
| if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() { | |
| surface.blit_to_framebuffer(fb_cfg); | |
| } | |
| // Enable CPU Hardware Interrupts now that all subsystems and initial desktop are ready | |
| x86_64::instructions::interrupts::enable(); | |
| println!("[+] Hardware Interrupts enabled. AeroOS Desktop interactive."); | |
| // Main Desktop Compositor Loop | |
| loop { | |
| // A. Poll Network Stack (handles packet RX/TX, ARP replies, ICMP ping) | |
| net::poll(); | |
| let mut full_dirty = false; | |
| let mut active_dirty = false; | |
| let mut mouse_dirty = false; | |
| let win_count_before = compositor.windows.len(); | |
| // B. Process Keyboard Events (Fast Path) | |
| while let Some(key) = drivers::ps2_keyboard::pop_key() { | |
| compositor.handle_key(key); | |
| active_dirty = true; | |
| } | |
| // If a window was closed (e.g. Escape key), trigger full desktop render | |
| if compositor.windows.len() != win_count_before { | |
| full_dirty = true; | |
| } | |
| // Process pending GUI app launch requests | |
| if let Some(app) = apps::pop_app_launch() { | |
| desktop.launch_app(app, &mut compositor); | |
| full_dirty = true; | |
| } | |
| // C. Process Mouse Events | |
| let mouse_state = ps2_mouse::get_mouse_state(); | |
| let mouse_x = mouse_state.x; | |
| let mouse_y = mouse_state.y; | |
| if mouse_x != last_mouse_x || mouse_y != last_mouse_y { | |
| mouse_dirty = true; | |
| } | |
| // Process Scroll Wheel / Touchpad Scrolling (Fast Path) | |
| if mouse_state.scroll_delta != 0 { | |
| if compositor.handle_scroll(mouse_x, mouse_y, mouse_state.scroll_delta) { | |
| active_dirty = true; | |
| } | |
| } | |
| // Mouse Down (Edge trigger) | |
| if mouse_state.left_button && !last_left_button { | |
| full_dirty = true; | |
| desktop.handle_click( | |
| mouse_x, | |
| mouse_y, | |
| &mut compositor, | |
| screen_w, | |
| screen_h, | |
| ); | |
| } else if !mouse_state.left_button && last_left_button { | |
| // Mouse Up | |
| full_dirty = true; | |
| compositor.handle_mouse_up(); | |
| } | |
| // Mouse Drag / Movement | |
| if mouse_state.left_button && compositor.dragging.is_some() && mouse_dirty { | |
| compositor.handle_mouse_move(mouse_x, mouse_y); | |
| full_dirty = true; | |
| } | |
| last_left_button = mouse_state.left_button; | |
| // Check system clock / timer ticks for UI updates (e.g. clock in taskbar) | |
| let ticks = arch::idt::SYSTEM_TICKS.load(core::sync::atomic::Ordering::Relaxed); | |
| let mut clock_dirty = false; | |
| if ticks / 100 != last_ticks / 100 { | |
| last_ticks = ticks; | |
| clock_dirty = true; | |
| } | |
| if full_dirty { | |
| desktop.render(&mut desktop_surface, &mut compositor); | |
| surface.copy_from(&desktop_surface); | |
| surface.draw_mouse_cursor(mouse_x, mouse_y); | |
| if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() { | |
| surface.blit_to_framebuffer(fb_cfg); | |
| } | |
| last_mouse_x = mouse_x; | |
| last_mouse_y = mouse_y; | |
| } else if active_dirty { | |
| // High-Performance Fast Path for Typing & Scrolling (Zero Lag): | |
| // Redraw only the active window's client area and blit only that rectangle to VRAM! | |
| if let Some((cx, cy, cw, ch)) = compositor.render_active_client(&mut desktop_surface) { | |
| surface.copy_rect_from(&desktop_surface, cx, cy, cw, ch); | |
| if mouse_x >= cx && mouse_x < cx + cw as i32 && mouse_y >= cy && mouse_y < cy + ch as i32 { | |
| surface.draw_mouse_cursor(mouse_x, mouse_y); | |
| } | |
| if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() { | |
| surface.blit_rect_to_framebuffer(fb_cfg, cx, cy, cw, ch); | |
| } | |
| } | |
| } else if clock_dirty { | |
| // Fast Path: Only redraw and blit the taskbar tray when the second ticks | |
| desktop::taskbar::render_taskbar(&mut desktop_surface, &mut compositor); | |
| let tb_y = (screen_h - desktop::taskbar::TASKBAR_HEIGHT) as i32; | |
| surface.copy_rect_from(&desktop_surface, 0, tb_y, screen_w, desktop::taskbar::TASKBAR_HEIGHT); | |
| if mouse_y >= tb_y { | |
| surface.draw_mouse_cursor(mouse_x, mouse_y); | |
| } | |
| if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() { | |
| surface.blit_rect_to_framebuffer(fb_cfg, 0, tb_y, screen_w, desktop::taskbar::TASKBAR_HEIGHT); | |
| } | |
| } else if mouse_dirty { | |
| // Instant Sub-Millisecond Cursor Update: | |
| // Only restore old cursor bounding box (16x22) and draw new cursor bounding box! | |
| let cw = 16; | |
| let ch = 22; | |
| surface.copy_rect_from(&desktop_surface, last_mouse_x, last_mouse_y, cw, ch); | |
| surface.copy_rect_from(&desktop_surface, mouse_x, mouse_y, cw, ch); | |
| surface.draw_mouse_cursor(mouse_x, mouse_y); | |
| if let Some(ref mut fb_cfg) = *FRAMEBUFFER.lock() { | |
| surface.blit_rect_to_framebuffer(fb_cfg, last_mouse_x, last_mouse_y, cw, ch); | |
| surface.blit_rect_to_framebuffer(fb_cfg, mouse_x, mouse_y, cw, ch); | |
| } | |
| last_mouse_x = mouse_x; | |
| last_mouse_y = mouse_y; | |
| } else { | |
| // Halt CPU until next hardware interrupt (Timer, Keyboard, Mouse) | |
| x86_64::instructions::hlt(); | |
| } | |
| } | |
| } | |
| fn panic(info: &PanicInfo) -> ! { | |
| println!("\n[!] KERNEL PANIC: {}", info); | |
| loop { | |
| x86_64::instructions::hlt(); | |
| } | |
| } | |