Skip to content

Commit

Permalink
add draft to support WebAssembly within the kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Jun 21, 2024
1 parent 2143b3b commit 19eb816
Show file tree
Hide file tree
Showing 11 changed files with 1,076 additions and 3 deletions.
663 changes: 661 additions & 2 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ nostd = []
semihosting = ["dep:semihosting"]
shell = ["simple-shell"]
idle-poll = []
wasm = ["wasmtime"]

[dependencies]
hermit-macro = { path = "hermit-macro" }
Expand Down Expand Up @@ -107,6 +108,7 @@ talc = { version = "4" }
time = { version = "0.3", default-features = false }
volatile = { version = "0.6", features = ["unstable"] }
zerocopy = { version = "0.7", features = ["derive"] }
wasmtime = { version = "21.0", default-features = false, features = ["runtime", "gc", "component-model"], optional = true }

[dependencies.smoltcp]
version = "0.11"
Expand Down
14 changes: 14 additions & 0 deletions src/arch/x86_64/kernel/longjmp.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.section .text
.global longjmp
longjmp:
xor eax,eax
cmp esi, 1 /* CF = val ? 0 : 1 */
adc eax, esi /* eax = val + !val */
mov rbx, [rdi]
mov rbp, [rdi+8]
mov r12, [rdi+16]
mov r13, [rdi+24]
mov r14, [rdi+32]
mov r15, [rdi+40]
mov rsp, [rdi+48]
jmp [rdi+56]
4 changes: 4 additions & 0 deletions src/arch/x86_64/kernel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "common-os")]
use core::arch::asm;
use core::arch::global_asm;
use core::num::NonZeroU64;
use core::ptr;
use core::sync::atomic::{AtomicPtr, AtomicU32, Ordering};
Expand Down Expand Up @@ -38,6 +39,9 @@ pub(crate) mod systemtime;
#[cfg(feature = "vga")]
mod vga;

global_asm!(include_str!("setjmp.s"));
global_asm!(include_str!("longjmp.s"));

/// Kernel header to announce machine features
#[cfg_attr(target_os = "none", link_section = ".data")]
static mut RAW_BOOT_INFO: Option<&'static RawBootInfo> = None;
Expand Down
15 changes: 15 additions & 0 deletions src/arch/x86_64/kernel/setjmp.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.section .text
.global setjmp
setjmp:
mov [rdi], rbx
mov [rdi+8], rbp
mov [rdi+16], r12
mov [rdi+24], r13
mov [rdi+32], r14
mov [rdi+40], r15
lea rdx, [rsp+8] # rsp without current ret addr
mov [rdi+48], rdx
mov rdi, rsp # save return addr ptr for new rip
mov [rdi+56], rdx
xor rax, rax
ret
2 changes: 1 addition & 1 deletion src/arch/x86_64/mm/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn virtual_to_physical(virtual_address: VirtAddr) -> Option<PhysAddr> {

match translate {
TranslateResult::NotMapped | TranslateResult::InvalidFrameAddress(_) => {
warn!(
trace!(
"Uable to determine the physical address of 0x{:X}",
virtual_address
);
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ mod shell;
mod synch;
pub mod syscalls;
pub mod time;
#[cfg(all(target_arch = "x86_64", feature = "wasm"))]
mod wasm;

#[cfg(target_os = "none")]
hermit_entry::define_entry_version!();
Expand Down Expand Up @@ -156,6 +158,11 @@ extern "C" fn initd(_arg: usize) {
#[cfg(not(test))]
let (argc, argv, environ) = syscalls::get_application_parameters();

#[cfg(all(target_arch = "x86_64", feature = "wasm"))]
if crate::wasm::init().is_err() {
error!("Unable to initialized wasm support")
}

// give the IP thread time to initialize the network interface
core_scheduler().reschedule();

Expand Down
Loading

0 comments on commit 19eb816

Please sign in to comment.