//! AbleOS Kernel Entrypoint use { crate::{ arch::hardware_random_u64, bootmodules::BootModules, //bootmodules::build_cmd, device_tree::DeviceTree, holeybytes::ExecThread, ipc::buffer::IpcBuffer, task::Executor, }, alloc::boxed::Box, core::cell::LazyCell, hashbrown::HashMap, hbvm::mem::Address, limine::{Framebuffer, FramebufferRequest, NonNullPtr}, log::{debug, error, trace}, spin::{Lazy, Mutex}, }; pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! { debug!("Entered kmain"); #[cfg(feature = "ktest")] { use crate::ktest; debug!("TESTING"); ktest::test_main(); loop {} } // let kcmd = build_cmd("Kernel Command Line", cmdline); // trace!("Cmdline: {kcmd:?}"); // for (i, bm) in boot_modules.iter().enumerate() { // let name = format!("module-{}", i); // let _bmcmd: XMLElement; // if bm.cmd.len() >= 2 { // // TODO: pass into the program // // Pass CMDLine into an IPCBuffer and put the ptr to the IPCBuffer in r200 // _bmcmd = build_cmd(name, bm.cmd.clone()); // log::info!("{:?}", _bmcmd); // } // } let dt = DEVICE_TREE.lock(); // TODO(Able): This line causes a deadlock debug!("Device Tree: {}", dt); trace!("Boot complete. Moving to init_system"); // TODO: schedule the disk driver from the initramfs // TODO: schedule the filesystem driver from the initramfs // TODO: Schedule the VFS from initramfs // TODO: schedule the init system from the initramfs drop(dt); let fb1: &NonNullPtr = &FB_REQ.get_response().get().unwrap().framebuffers()[0]; { let mut dt = DEVICE_TREE.lock(); let mut disp = xml::XMLElement::new("display_0"); disp.set_attribute("width", fb1.width); disp.set_attribute("height", fb1.height); disp.set_attribute("bpp", fb1.bpp); disp.set_attribute("pitch", fb1.pitch); dt.devices.insert("Displays", alloc::vec![disp]); } debug!("Graphics initialised"); debug!( "Graphics front ptr {:?}", fb1.address.as_ptr().unwrap() as *const u8 ); unsafe { let executor = LazyCell::::force_mut(&mut EXECUTOR); for module in boot_modules.iter() { let cmd = module.cmd.trim_matches('"'); let cmd_len = cmd.len() as u64; log::info!( "Starting {}", module .path .split('/') .last() .unwrap() .split('.') .next() .unwrap() ); log::debug!("Spawning {} with arguments \"{}\"", module.path, cmd); // decode AbleOS Executable format let header = &module.bytes[0..46]; let magic_slice = &header[0..3]; if magic_slice != [0x15, 0x91, 0xD2] { log::error!("Invalid magic number at the start of executable."); continue; } let executable_format_version = u32::from_le_bytes(header[3..7].try_into().unwrap()); let offset = if executable_format_version == 0 { 47 } else { error!("Invalid executable format."); continue; }; let code_length = u64::from_le_bytes(header[7..15].try_into().unwrap()); let data_length = u64::from_le_bytes(header[15..23].try_into().unwrap()); let end = (code_length + data_length) as usize; log::debug!("{code_length} + {data_length} = {end}"); let mut thr = ExecThread::new(&module.bytes[offset..end], Address::new(0)); if cmd_len > 0 { thr.set_arguments(cmd.as_ptr() as u64, cmd_len); } executor.spawn(Box::pin(async move { if let Err(e) = thr.await { log::error!("{e:?}"); } })); } debug!("Random number: {}", hardware_random_u64()); executor.run(); }; log::info!("Started AbleOS"); crate::arch::spin_loop() } // ! SAFETY: this is not threadsafe at all, like even a little bit. // ! SERIOUSLY pub static mut EXECUTOR: LazyCell = LazyCell::new(|| Executor::new()); pub static DEVICE_TREE: Lazy> = Lazy::new(|| { let dt = DeviceTree::new(); Mutex::new(dt) }); pub static FB_REQ: FramebufferRequest = FramebufferRequest::new(0); pub type IpcBuffers<'a> = HashMap>; pub static IPC_BUFFERS: Lazy> = Lazy::new(|| { let mut bufs = HashMap::new(); let log_buffer = IpcBuffer::new(false, 0); let file_buffer = IpcBuffer::new(false, 0); bufs.insert(1, log_buffer); bufs.insert(2, file_buffer); Mutex::new(bufs) });