forked from AbleOS/ableos
84 lines
2.1 KiB
Rust
84 lines
2.1 KiB
Rust
use {
|
|
alloc::boxed::Box,
|
|
core::{future::Future, task::Poll},
|
|
hbvm::{
|
|
mem::{
|
|
softpaging::{icache::ICache, HandlePageFault, SoftPagedMem},
|
|
Address,
|
|
},
|
|
Vm, VmRunError, VmRunOk,
|
|
},
|
|
};
|
|
|
|
const TIMER_QUOTIENT: usize = 100;
|
|
|
|
pub struct ExecThread<'p> {
|
|
vm: Vm<SoftPagedMem<'p, PageFaultHandler, true>, TIMER_QUOTIENT>,
|
|
}
|
|
|
|
unsafe impl<'p> Send for ExecThread<'p> {}
|
|
|
|
impl<'p> ExecThread<'p> {
|
|
pub unsafe fn new(program: &'p [u8], entrypoint: Address) -> Self {
|
|
ExecThread {
|
|
vm: unsafe {
|
|
Vm::new(
|
|
SoftPagedMem {
|
|
root_pt: Box::into_raw(Box::default()),
|
|
pf_handler: PageFaultHandler,
|
|
program,
|
|
icache: ICache::default(),
|
|
},
|
|
entrypoint,
|
|
)
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'p> Future for ExecThread<'p> {
|
|
type Output = Result<(), VmRunError>;
|
|
|
|
fn poll(
|
|
mut self: core::pin::Pin<&mut Self>,
|
|
cx: &mut core::task::Context<'_>,
|
|
) -> Poll<Self::Output> {
|
|
match self.vm.run() {
|
|
Err(err) => return Poll::Ready(Err(err)),
|
|
Ok(VmRunOk::End) => return Poll::Ready(Ok(())),
|
|
Ok(VmRunOk::Ecall) => {
|
|
log::info!("Ecall");
|
|
log::info!("{:?}", self.vm.registers);
|
|
}
|
|
Ok(VmRunOk::Timer) => (),
|
|
}
|
|
|
|
cx.waker().wake_by_ref();
|
|
Poll::Pending
|
|
}
|
|
}
|
|
|
|
struct PageFaultHandler;
|
|
impl HandlePageFault for PageFaultHandler {
|
|
fn page_fault(
|
|
&mut self,
|
|
reason: hbvm::mem::MemoryAccessReason,
|
|
pagetable: &mut hbvm::mem::softpaging::paging::PageTable,
|
|
vaddr: hbvm::mem::Address,
|
|
size: hbvm::mem::softpaging::PageSize,
|
|
dataptr: *mut u8,
|
|
) -> bool
|
|
where
|
|
Self: Sized,
|
|
{
|
|
log::error!(
|
|
"REASON: {reason} \
|
|
vaddr: {vaddr} \
|
|
size: {size:?} \
|
|
Dataptr {dataptr:p}",
|
|
);
|
|
|
|
false
|
|
}
|
|
}
|