Next Generation WASM Microkernel Operating System
at trap_handler 146 lines 4.7 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 8mod builtins; 9mod code_object; 10mod const_eval; 11mod instance; 12mod instance_alloc; 13mod memory; 14mod mmap_vec; 15mod provenance; 16mod table; 17mod vmcontext; 18mod vmshape; 19 20use alloc::vec::Vec; 21use core::ptr::NonNull; 22 23use crate::wasm::indices::DefinedMemoryIndex; 24use crate::wasm::translate; 25use crate::wasm::translate::TranslatedModule; 26pub use code_object::CodeObject; 27pub use const_eval::ConstExprEvaluator; 28pub use instance::{Instance, InstanceAndStore, InstanceHandle}; 29pub use instance_alloc::InstanceAllocator; 30#[cfg(test)] 31pub use instance_alloc::PlaceholderAllocatorDontUse; 32pub use memory::Memory; 33pub use mmap_vec::MmapVec; 34pub use provenance::VmPtr; 35pub use table::{Table, TableElement}; 36pub use vmcontext::*; 37pub use vmshape::{StaticVMShape, VMShape}; 38 39/// The value of an export passed from one instance to another. 40#[derive(Debug, Clone)] 41pub enum Export { 42 /// A function export value. 43 Function(ExportedFunction), 44 /// A table export value. 45 Table(ExportedTable), 46 /// A memory export value. 47 Memory(ExportedMemory), 48 /// A global export value. 49 Global(ExportedGlobal), 50 /// A tag export value. 51 Tag(ExportedTag), 52} 53 54/// A function export value. 55#[derive(Debug, Clone)] 56pub struct ExportedFunction { 57 /// 58 /// Note that exported functions cannot be a null funcref, so this is a 59 /// non-null pointer. 60 pub func_ref: NonNull<VMFuncRef>, 61} 62// Safety: As part of the contract for using `ExportFunction`, synchronization 63// properties must be upheld. Therefore, despite containing raw pointers, 64// it is declared as Send/Sync. 65unsafe impl Send for ExportedFunction {} 66// Safety: see above 67unsafe impl Sync for ExportedFunction {} 68 69#[derive(Debug, Clone)] 70pub struct ExportedTable { 71 /// The address of the table descriptor. 72 pub definition: NonNull<VMTableDefinition>, 73 /// Pointer to the containing `VMContext`. 74 pub vmctx: NonNull<VMContext>, 75 pub table: translate::Table, 76} 77// Safety: see docs on send/sync for `ExportedFunction` above. 78unsafe impl Send for ExportedTable {} 79// Safety: see docs on send/sync for `ExportedFunction` above. 80unsafe impl Sync for ExportedTable {} 81 82/// A memory export value. 83#[derive(Debug, Clone)] 84pub struct ExportedMemory { 85 /// The address of the memory descriptor. 86 pub definition: NonNull<VMMemoryDefinition>, 87 /// Pointer to the containing `VMContext`. 88 pub vmctx: NonNull<VMContext>, 89 /// The index at which the memory is defined within the `vmctx`. 90 pub index: DefinedMemoryIndex, 91 pub memory: translate::Memory, 92} 93// Safety: see docs on send/sync for `ExportedFunction` above. 94unsafe impl Send for ExportedMemory {} 95// Safety: see docs on send/sync for `ExportedFunction` above. 96unsafe impl Sync for ExportedMemory {} 97 98/// A global export value. 99#[derive(Debug, Clone)] 100pub struct ExportedGlobal { 101 /// The address of the global storage. 102 pub definition: NonNull<VMGlobalDefinition>, 103 /// Pointer to the containing `VMContext`. May be null for host-created 104 /// globals. 105 pub vmctx: Option<NonNull<VMContext>>, 106 pub global: translate::Global, 107} 108// Safety: see docs on send/sync for `ExportedFunction` above. 109unsafe impl Send for ExportedGlobal {} 110// Safety: see docs on send/sync for `ExportedFunction` above. 111unsafe impl Sync for ExportedGlobal {} 112 113/// A tag export value. 114#[derive(Debug, Clone)] 115pub struct ExportedTag { 116 /// The address of the global storage. 117 pub definition: NonNull<VMTagDefinition>, 118 pub tag: translate::Tag, 119} 120// Safety: see docs on send/sync for `ExportedFunction` above. 121unsafe impl Send for ExportedTag {} 122// Safety: see docs on send/sync for `ExportedFunction` above. 123unsafe impl Sync for ExportedTag {} 124 125#[derive(Debug, Default)] 126pub struct Imports { 127 pub functions: Vec<VMFunctionImport>, 128 pub tables: Vec<VMTableImport>, 129 pub memories: Vec<VMMemoryImport>, 130 pub globals: Vec<VMGlobalImport>, 131 pub tags: Vec<VMTagImport>, 132} 133 134impl Imports { 135 pub(crate) fn with_capacity_for(raw: &TranslatedModule) -> Self { 136 let mut this = Self::default(); 137 138 this.functions.reserve(raw.num_imported_functions as usize); 139 this.tables.reserve(raw.num_imported_tables as usize); 140 this.memories.reserve(raw.num_imported_memories as usize); 141 this.globals.reserve(raw.num_imported_globals as usize); 142 this.tags.reserve(raw.num_imported_tags as usize); 143 144 this 145 } 146}