Stack for programs

This commit is contained in:
Erin 2023-11-15 19:33:21 +01:00
parent ebf31cba3d
commit 0f7525b7e3
2 changed files with 35 additions and 8 deletions

4
Cargo.lock generated
View file

@ -467,7 +467,7 @@ dependencies = [
[[package]]
name = "hbbytecode"
version = "0.1.0"
source = "git+https://git.ablecorp.us/ableos/holey-bytes#0f619887c613be3fb3a7dd8c970b7ee76ad0d23b"
source = "git+https://git.ablecorp.us/ableos/holey-bytes#4fb203aa4d96483a3cfd5890717e96d00f9b5c3f"
dependencies = [
"with_builtin_macros",
]
@ -475,7 +475,7 @@ dependencies = [
[[package]]
name = "hbvm"
version = "0.1.0"
source = "git+https://git.ablecorp.us/ableos/holey-bytes#0f619887c613be3fb3a7dd8c970b7ee76ad0d23b"
source = "git+https://git.ablecorp.us/ableos/holey-bytes#4fb203aa4d96483a3cfd5890717e96d00f9b5c3f"
dependencies = [
"hbbytecode",
]

View file

@ -1,3 +1,5 @@
use core::ptr::NonNull;
mod ecah;
mod mem;
@ -20,6 +22,7 @@ type Vm = hbvm::Vm<mem::Memory, TIMER_QUOTIENT>;
pub struct ExecThread<'p> {
vm: Vm,
stack_top: *mut u8,
_phantom: PhantomData<&'p [u8]>,
}
@ -31,18 +34,30 @@ impl<'p> ExecThread<'p> {
}
pub unsafe fn new(program: &'p [u8], entrypoint: Address) -> Self {
let mut vm = unsafe {
Vm::new(
mem::Memory,
Address::new(program.as_ptr() as u64 + entrypoint.get()),
)
};
let stack_top = allocate_stack().as_ptr();
vm.write_reg(254, stack_top as u64);
ExecThread {
vm: unsafe {
Vm::new(
mem::Memory,
Address::new(program.as_ptr() as u64 + entrypoint.get()),
)
},
vm,
stack_top,
_phantom: Default::default(),
}
}
}
impl<'p> Drop for ExecThread<'p> {
fn drop(&mut self) {
unsafe { alloc::alloc::dealloc(self.stack_top, stack_layout()) };
}
}
impl<'p> Future for ExecThread<'p> {
type Output = Result<(), VmRunError>;
@ -94,3 +109,15 @@ impl HandlePageFault for PageFaultHandler {
false
}
}
const fn stack_layout() -> core::alloc::Layout {
unsafe { alloc::alloc::Layout::from_size_align_unchecked(1024 * 1024, 4096) }
}
fn allocate_stack() -> NonNull<u8> {
let layout = stack_layout();
match NonNull::new(unsafe { alloc::alloc::alloc_zeroed(layout) }) {
Some(ptr) => ptr,
None => alloc::alloc::handle_alloc_error(layout),
}
}