1
0
Fork 0
forked from AbleOS/ableos
ableos/kernel/src/kmain.rs
2024-09-19 06:05:11 -05:00

138 lines
4.2 KiB
Rust

//! AbleOS Kernel Entrypoint
use {
crate::{
arch::hardware_random_u64,
bootmodules::BootModules,
//bootmodules::build_cmd,
device_tree::DeviceTree,
holeybytes::ExecThread,
ipc::buffer::IpcBuffer,
},
hashbrown::HashMap,
hbvm::mem::Address,
limine::{Framebuffer, FramebufferRequest, NonNullPtr},
log::{debug, trace},
spin::{Lazy, Mutex},
};
pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! {
debug!("Entered kmain");
// 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<Framebuffer> = &FB_REQ.get_response().get().unwrap().framebuffers()[0];
{
use crate::alloc::string::ToString;
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("bits per pixel", fb1.bpp);
disp.set_attribute("pitch", fb1.pitch);
dt.devices.insert("Displays".to_string(), alloc::vec![disp]);
}
debug!("Graphics initialised");
debug!(
"Graphics front ptr {:?}",
fb1.address.as_ptr().unwrap() as *const u8
);
let mut executor = crate::task::Executor::new(256);
unsafe {
for module in boot_modules.iter() {
let cmd = module.cmd.trim_matches('"');
let cmd_len = cmd.len() as u64;
log::info!("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::warn!("Non-AbleOS Executable was attempted to be run.")
}
let executable_format_version = u32::from_be_bytes(header[3..7].try_into().unwrap());
let offset = if executable_format_version == 0 {
47
} else {
0
};
// let code_length = u64::from_be_bytes(header[7..15].try_into().unwrap());
// let data_length = u64::from_be_bytes(header[15..23].try_into().unwrap());
// let end = (code_length + data_length) as usize;
let mut thr = ExecThread::new(&module.bytes[offset..], Address::new(0));
if cmd_len > 0 {
thr.set_arguments(cmd.as_ptr() as u64, cmd_len);
}
executor.spawn(async move {
if let Err(e) = thr.await {
log::error!("{e:?}");
}
})
}
debug!("Random number: {}", hardware_random_u64());
executor.run();
};
crate::arch::spin_loop()
}
pub static DEVICE_TREE: Lazy<Mutex<DeviceTree>> = Lazy::new(|| {
let dt = DeviceTree::new();
Mutex::new(dt)
});
pub static FB_REQ: FramebufferRequest = FramebufferRequest::new(0);
pub type IpcBuffers<'a> = HashMap<u64, IpcBuffer<'a>>;
pub static IPC_BUFFERS: Lazy<Mutex<IpcBuffers>> = 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)
});
#[test_case]
fn trivial_assertion() {
trace!("trivial assertion... ");
assert_eq!(1, 1);
info!("[ok]");
}