ableos/kernel/src/kmain.rs

158 lines
4.8 KiB
Rust
Raw Normal View History

2023-03-30 16:43:04 -05:00
//! AbleOS Kernel Entrypoint
2023-05-06 06:50:24 -05:00
use {
2023-07-08 23:22:44 -05:00
crate::{
2024-09-13 16:41:31 -05:00
arch::hardware_random_u64,
bootmodules::BootModules,
//bootmodules::build_cmd,
2023-07-08 23:22:44 -05:00
device_tree::DeviceTree,
holeybytes::ExecThread,
2024-09-13 16:41:31 -05:00
ipc::buffer::IpcBuffer,
2024-11-17 14:29:32 -06:00
task::Executor,
2023-07-08 23:22:44 -05:00
},
2024-11-17 14:29:32 -06:00
alloc::boxed::Box,
core::cell::LazyCell,
hashbrown::HashMap,
hbvm::mem::Address,
limine::{Framebuffer, FramebufferRequest, NonNullPtr},
2024-09-19 07:11:57 -05:00
log::{debug, error, trace},
2023-05-06 06:50:24 -05:00
spin::{Lazy, Mutex},
};
2023-04-05 12:29:20 -05:00
2024-09-13 16:41:31 -05:00
pub fn kmain(_cmdline: &str, boot_modules: BootModules) -> ! {
2023-05-25 07:04:19 -05:00
debug!("Entered kmain");
2023-03-30 16:43:04 -05:00
2024-09-13 16:41:31 -05:00
// let kcmd = build_cmd("Kernel Command Line", cmdline);
// trace!("Cmdline: {kcmd:?}");
2023-03-30 16:43:04 -05:00
2023-11-18 01:32:09 -06:00
// 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);
// }
// }
2023-07-08 23:22:44 -05:00
2023-05-23 05:16:14 -05:00
let dt = DEVICE_TREE.lock();
2023-04-05 12:29:20 -05:00
2023-09-13 02:19:37 -05:00
// TODO(Able): This line causes a deadlock
2024-09-15 11:01:29 -05:00
debug!("Device Tree: {}", dt);
2023-09-13 02:19:37 -05:00
2024-09-15 11:01:29 -05:00
trace!("Boot complete. Moving to init_system");
2023-06-25 22:34:24 -05:00
// TODO: schedule the disk driver from the initramfs
// TODO: schedule the filesystem driver from the initramfs
// TODO: Schedule the VFS from initramfs
2023-06-25 22:34:24 -05:00
// TODO: schedule the init system from the initramfs
2023-05-15 02:19:34 -05:00
drop(dt);
let fb1: &NonNullPtr<Framebuffer> = &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);
2024-09-19 14:40:10 -05:00
disp.set_attribute("bpp", fb1.bpp);
disp.set_attribute("pitch", fb1.pitch);
dt.devices.insert("Displays", alloc::vec![disp]);
}
2024-09-15 11:01:29 -05:00
debug!("Graphics initialised");
debug!(
"Graphics front ptr {:?}",
fb1.address.as_ptr().unwrap() as *const u8
);
2023-08-22 08:52:30 -05:00
unsafe {
2024-11-17 14:29:32 -06:00
let executor = LazyCell::<Executor>::force_mut(&mut EXECUTOR);
2024-09-15 11:01:29 -05:00
for module in boot_modules.iter() {
let cmd = module.cmd.trim_matches('"');
2024-09-13 16:41:31 -05:00
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);
2024-09-19 06:05:11 -05:00
// decode AbleOS Executable format
let header = &module.bytes[0..46];
let magic_slice = &header[0..3];
if magic_slice != [0x15, 0x91, 0xD2] {
2024-09-19 07:11:57 -05:00
log::error!("Invalid magic number at the start of executable.");
continue;
2024-09-19 06:05:11 -05:00
}
2024-09-19 07:11:57 -05:00
let executable_format_version = u32::from_le_bytes(header[3..7].try_into().unwrap());
2024-09-19 06:05:11 -05:00
let offset = if executable_format_version == 0 {
47
} else {
2024-09-19 07:11:57 -05:00
error!("Invalid executable format.");
continue;
2024-09-19 06:05:11 -05:00
};
2024-09-19 07:11:57 -05:00
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}");
2024-09-19 06:05:11 -05:00
2024-09-19 07:11:57 -05:00
let mut thr = ExecThread::new(&module.bytes[offset..end], Address::new(0));
2024-09-15 11:01:29 -05:00
if cmd_len > 0 {
thr.set_arguments(cmd.as_ptr() as u64, cmd_len);
}
2024-11-17 14:29:32 -06:00
executor.spawn(Box::pin(async move {
if let Err(e) = thr.await {
2023-08-22 08:52:30 -05:00
log::error!("{e:?}");
}
2024-11-17 14:29:32 -06:00
}));
2023-08-22 08:52:30 -05:00
}
2024-09-15 11:01:29 -05:00
debug!("Random number: {}", hardware_random_u64());
2024-02-15 14:21:00 -06:00
2023-08-22 08:52:30 -05:00
executor.run();
};
2023-07-08 23:22:44 -05:00
2023-08-22 08:52:30 -05:00
crate::arch::spin_loop()
2023-03-30 16:43:04 -05:00
}
2023-04-10 01:16:30 -05:00
2024-11-17 14:29:32 -06:00
// ! SAFETY: this is not threadsafe at all, like even a little bit.
// ! SERIOUSLY
pub static mut EXECUTOR: LazyCell<Executor> = LazyCell::new(|| Executor::new());
2023-05-06 06:50:24 -05:00
pub static DEVICE_TREE: Lazy<Mutex<DeviceTree>> = Lazy::new(|| {
2023-05-23 05:16:14 -05:00
let dt = DeviceTree::new();
2023-05-06 06:50:24 -05:00
Mutex::new(dt)
});
pub static FB_REQ: FramebufferRequest = FramebufferRequest::new(0);
2023-05-15 02:19:34 -05:00
2024-09-13 16:41:31 -05:00
pub type IpcBuffers<'a> = HashMap<u64, IpcBuffer<'a>>;
2023-10-23 09:12:43 -05:00
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)
});
2023-05-15 02:19:34 -05:00
#[test_case]
fn trivial_assertion() {
trace!("trivial assertion... ");
assert_eq!(1, 1);
info!("[ok]");
}