ableos/kernel/src/holeybytes/mod.rs

107 lines
2.7 KiB
Rust
Raw Normal View History

2023-10-27 20:26:04 -05:00
mod ecah;
2024-03-11 09:48:56 -05:00
mod kernel_services;
2023-10-27 20:26:04 -05:00
mod mem;
2023-08-22 08:52:30 -05:00
use {
2024-09-16 14:15:51 -05:00
alloc::alloc::{alloc, dealloc},
2024-09-13 16:41:31 -05:00
core::{
alloc::Layout,
future::Future,
pin::Pin,
task::{Context, Poll},
},
2023-08-22 08:57:57 -05:00
hbvm::{
2024-09-13 16:41:31 -05:00
mem::{softpaging::HandlePageFault, Address},
2023-10-27 20:26:04 -05:00
VmRunError, VmRunOk,
2023-08-22 08:57:57 -05:00
},
2024-09-13 16:41:31 -05:00
log::error,
2023-08-22 08:52:30 -05:00
};
2023-11-15 12:37:52 -06:00
const STACK_SIZE: usize = 1024 * 1024;
2024-09-13 16:41:31 -05:00
const TIMER_QUOTIENT: usize = 1000;
2023-10-27 20:26:04 -05:00
type Vm = hbvm::Vm<mem::Memory, TIMER_QUOTIENT>;
2023-08-22 08:52:30 -05:00
2024-09-13 16:41:31 -05:00
pub struct ExecThread {
2023-10-27 20:26:04 -05:00
vm: Vm,
2023-11-15 12:41:44 -06:00
stack_bottom: *mut u8,
2023-08-22 08:52:30 -05:00
}
2024-09-13 16:41:31 -05:00
unsafe impl Send for ExecThread {}
impl ExecThread {
pub fn set_arguments(&mut self, ptr: u64, length: u64) {
self.vm.registers[1] = hbvm::value::Value(ptr);
self.vm.registers[2] = hbvm::value::Value(length);
}
2024-09-13 16:41:31 -05:00
pub unsafe fn new(program: &[u8], entrypoint: Address) -> Self {
let mut vm = Vm::new(
mem::Memory {},
Address::new(program.as_ptr() as u64 + entrypoint.get()),
);
let stack_bottom = allocate_stack();
2023-11-15 12:33:21 -06:00
2023-11-15 12:41:44 -06:00
vm.write_reg(254, (stack_bottom as usize + STACK_SIZE - 1) as u64);
2023-11-15 12:33:21 -06:00
2024-09-13 16:41:31 -05:00
ExecThread { vm, stack_bottom }
2023-08-22 08:52:30 -05:00
}
}
2024-09-13 16:41:31 -05:00
impl<'p> Drop for ExecThread {
2023-11-15 12:33:21 -06:00
fn drop(&mut self) {
2024-09-13 16:41:31 -05:00
unsafe { dealloc(self.stack_bottom, stack_layout()) };
2023-11-15 12:33:21 -06:00
}
}
2024-09-13 16:41:31 -05:00
impl<'p> Future for ExecThread {
2023-08-22 08:52:30 -05:00
type Output = Result<(), VmRunError>;
2024-09-13 16:41:31 -05:00
#[inline(always)]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2023-08-22 08:52:30 -05:00
match self.vm.run() {
Err(err) => {
2024-09-15 11:01:29 -05:00
log::error!("HBVM Error\r\nRegister dump: {:?}", self.vm.registers,);
return Poll::Ready(Err(err));
}
2024-09-15 11:01:29 -05:00
Ok(VmRunOk::End) => return Poll::Ready(Ok(())),
Ok(VmRunOk::Ecall) => ecah::handler(&mut self.vm),
Ok(VmRunOk::Timer) => (),
2023-10-27 20:26:04 -05:00
Ok(VmRunOk::Breakpoint) => {
2024-09-15 11:01:29 -05:00
log::error!(
2023-10-27 20:26:04 -05:00
"HBVM Debug breakpoint\r\nRegister dump: {:?}",
2024-09-15 11:01:29 -05:00
self.vm.registers,
2023-10-27 20:26:04 -05:00
);
}
2023-08-22 08:52:30 -05:00
}
2024-09-15 11:01:29 -05:00
cx.waker().wake_by_ref();
Poll::Pending
2023-08-22 08:52:30 -05:00
}
}
struct PageFaultHandler;
impl HandlePageFault for PageFaultHandler {
fn page_fault(
&mut self,
reason: hbvm::mem::MemoryAccessReason,
2024-09-13 16:41:31 -05:00
_pagetable: &mut hbvm::mem::softpaging::paging::PageTable,
2023-08-22 08:52:30 -05:00
vaddr: hbvm::mem::Address,
size: hbvm::mem::softpaging::PageSize,
dataptr: *mut u8,
2024-09-13 16:41:31 -05:00
) -> bool {
error!("REASON: {reason} vaddr: {vaddr} size: {size:?} Dataptr {dataptr:p}");
2023-08-22 08:52:30 -05:00
false
}
}
2023-11-15 12:33:21 -06:00
2024-09-13 16:41:31 -05:00
#[inline(always)]
const fn stack_layout() -> Layout {
unsafe { Layout::from_size_align_unchecked(STACK_SIZE, 4096) }
2023-11-15 12:33:21 -06:00
}
2024-09-13 16:41:31 -05:00
#[inline(always)]
fn allocate_stack() -> *mut u8 {
2024-09-16 14:15:51 -05:00
unsafe { alloc(stack_layout()) }
2023-11-15 12:33:21 -06:00
}