Next Generation WASM Microkernel Operating System
at trap_handler 107 lines 3.5 kB view raw
1// Copyright 2025 Jonas Kruckenberg 2// 3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or 4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or 5// http://opensource.org/licenses/MIT>, at your option. This file may not be 6// copied, modified, or distributed except according to those terms. 7 8use crate::mem::{Mmap, VirtualAddress}; 9use crate::wasm::vm::VMMemoryDefinition; 10use crate::wasm::vm::provenance::VmPtr; 11use core::ptr::NonNull; 12use core::range::Range; 13 14#[derive(Debug)] 15pub struct Memory { 16 /// The underlying allocation backing this memory 17 mmap: Mmap, 18 // mem: Vec<u8, UserAllocator>, 19 /// The current length of this Wasm memory, in bytes. 20 len: usize, 21 /// The optional maximum accessible size, in bytes, for this linear memory. 22 /// 23 /// This **does not** include guard pages and might be smaller than `self.accessible` 24 /// since the underlying allocation is always a multiple of the host page size. 25 maximum: Option<usize>, 26 /// The log2 of this Wasm memory's page size, in bytes. 27 page_size_log2: u8, 28 /// Size in bytes of extra guard pages after the end to 29 /// optimize loads and stores with constant offsets. 30 offset_guard_size: usize, 31} 32 33impl Memory { 34 pub(crate) fn from_parts( 35 mmap: Mmap, 36 len: usize, 37 maximum: Option<usize>, 38 page_size_log2: u8, 39 offset_guard_size: usize, 40 ) -> Self { 41 Self { 42 mmap, 43 len, 44 maximum, 45 page_size_log2, 46 offset_guard_size, 47 } 48 } 49 50 pub fn byte_size(&self) -> usize { 51 self.len 52 } 53 54 pub fn wasm_accessible(&self) -> Range<VirtualAddress> { 55 self.mmap.range() 56 } 57 58 // /// Implementation of `memory.atomic.notify` for all memories. 59 // pub fn atomic_notify(&mut self, addr: u64, count: u32) -> Result<u32, Trap> { 60 // match self.as_shared_memory() { 61 // Some(m) => m.atomic_notify(addr, count), 62 // None => { 63 // validate_atomic_addr(&self.vmmemory(), addr, 4, 4)?; 64 // Ok(0) 65 // } 66 // } 67 // } 68 // 69 // /// Implementation of `memory.atomic.wait32` for all memories. 70 // pub fn atomic_wait32( 71 // &mut self, 72 // addr: u64, 73 // expected: u32, 74 // timeout: Option<Duration>, 75 // ) -> Result<WaitResult, Trap> { 76 // match self.as_shared_memory() { 77 // Some(m) => m.atomic_wait32(addr, expected, timeout), 78 // None => { 79 // validate_atomic_addr(&self.vmmemory(), addr, 4, 4)?; 80 // Err(Trap::AtomicWaitNonSharedMemory) 81 // } 82 // } 83 // } 84 // 85 // /// Implementation of `memory.atomic.wait64` for all memories. 86 // pub fn atomic_wait64( 87 // &mut self, 88 // addr: u64, 89 // expected: u64, 90 // timeout: Option<Duration>, 91 // ) -> Result<WaitResult, Trap> { 92 // match self.as_shared_memory() { 93 // Some(m) => m.atomic_wait64(addr, expected, timeout), 94 // None => { 95 // validate_atomic_addr(&self.vmmemory(), addr, 8, 8)?; 96 // Err(Trap::AtomicWaitNonSharedMemory) 97 // } 98 // } 99 // } 100 101 pub(crate) fn vmmemory_definition(&mut self) -> VMMemoryDefinition { 102 VMMemoryDefinition { 103 base: VmPtr::from(NonNull::new(self.mmap.as_mut_ptr()).unwrap()), 104 current_length: self.len.into(), 105 } 106 } 107}