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,
|
2023-11-13 23:51:30 -06:00
|
|
|
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,
|
2023-11-13 23:51:30 -06:00
|
|
|
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-11-24 10:39:24 -06:00
|
|
|
#[cfg(feature = "ktest")]
|
|
|
|
{
|
2024-11-27 02:09:15 -06:00
|
|
|
use {
|
|
|
|
crate::ktest,
|
|
|
|
log::info,
|
|
|
|
};
|
|
|
|
info!("Running tests");
|
2024-11-23 18:21:39 -06:00
|
|
|
ktest::test_main();
|
|
|
|
|
2024-11-24 10:39:24 -06:00
|
|
|
loop {}
|
2024-11-23 18:21:39 -06: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
|
2023-09-11 01:36:13 -05:00
|
|
|
// 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
|
|
|
|
2023-11-13 23:51:30 -06: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);
|
2023-11-13 23:51:30 -06:00
|
|
|
disp.set_attribute("pitch", fb1.pitch);
|
2024-09-30 15:45:57 -05:00
|
|
|
dt.devices.insert("Displays", alloc::vec![disp]);
|
2023-11-13 23:51:30 -06:00
|
|
|
}
|
2024-09-15 11:01:29 -05:00
|
|
|
debug!("Graphics initialised");
|
|
|
|
debug!(
|
2023-11-13 23:51:30 -06:00
|
|
|
"Graphics front ptr {:?}",
|
|
|
|
fb1.address.as_ptr().unwrap() as *const u8
|
|
|
|
);
|
2024-11-24 14:58:14 -06:00
|
|
|
log::info!("Started AbleOS");
|
2023-11-13 23:51:30 -06:00
|
|
|
|
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;
|
2023-11-13 23:51:30 -06:00
|
|
|
|
2024-11-11 15:48:43 -06:00
|
|
|
log::info!(
|
|
|
|
"Starting {}",
|
|
|
|
module
|
|
|
|
.path
|
|
|
|
.split('/')
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.split('.')
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
log::debug!("Spawning {} with arguments \"{}\"", module.path, cmd);
|
2023-11-13 23:51:30 -06:00
|
|
|
|
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;
|
2024-11-11 15:48:43 -06:00
|
|
|
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-26 13:32:19 -06:00
|
|
|
executor.spawn(Box::pin(async {
|
2023-11-13 23:51:30 -06:00
|
|
|
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
|
|
|
}
|
2023-07-15 05:51:19 -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();
|
|
|
|
};
|
|
|
|
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)
|
|
|
|
});
|
2023-11-13 23:51:30 -06:00
|
|
|
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(|| {
|
2023-10-29 07:27:10 -05:00
|
|
|
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);
|
|
|
|
|
2023-09-07 14:36:53 -05:00
|
|
|
Mutex::new(bufs)
|
|
|
|
});
|