From 0f7525b7e3dbcaaf73650ba481ebedeceefaeefa Mon Sep 17 00:00:00 2001 From: Erin Date: Wed, 15 Nov 2023 19:33:21 +0100 Subject: [PATCH] Stack for programs --- Cargo.lock | 4 ++-- kernel/src/holeybytes/mod.rs | 39 ++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6bf0825..068da5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/kernel/src/holeybytes/mod.rs b/kernel/src/holeybytes/mod.rs index 7161648..fa3bb72 100644 --- a/kernel/src/holeybytes/mod.rs +++ b/kernel/src/holeybytes/mod.rs @@ -1,3 +1,5 @@ +use core::ptr::NonNull; + mod ecah; mod mem; @@ -20,6 +22,7 @@ type Vm = hbvm::Vm; 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 { + let layout = stack_layout(); + match NonNull::new(unsafe { alloc::alloc::alloc_zeroed(layout) }) { + Some(ptr) => ptr, + None => alloc::alloc::handle_alloc_error(layout), + } +}