Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111// SPDX-License-Identifier: GPL-2.0-or-later// Copyright (C) 2026 Tsiry Sandratraina
use anyhow::{Context, Result, ensure};use std::{ borrow::Cow, cell::UnsafeCell, ops::Range, sync::atomic::{AtomicU8, AtomicU32, AtomicU64, Ordering},};
/// Shared linear memory may be accessed by another instance even while/// this vCPU is inside a host function. Never form a Rust byte slice over it.pub enum Ram<'a> { Private(&'a mut [u8]), Shared(&'a [UnsafeCell<u8>]),}
impl Ram<'_> { pub fn len(&self) -> usize { match self { Self::Private(b) => b.len(), Self::Shared(b) => b.len(), } } pub fn range(&self, p: usize, len: usize) -> Result<Range<usize>> { let end = p.checked_add(len).context("guest address overflow")?; ensure!(end <= self.len(), "guest address outside RAM"); Ok(p..end) } pub fn bytes(&self, p: usize, len: usize) -> Result<Cow<'_, [u8]>> { let r = self.range(p, len)?; Ok(match self { Self::Private(b) => Cow::Borrowed(&b[r]), Self::Shared(b) => Cow::Owned( b[r].iter() .map(|v| atomic_byte(v).load(Ordering::Relaxed)) .collect(), ), }) } pub fn read<const N: usize>(&self, p: usize) -> Result<[u8; N]> { let r = self.range(p, N)?; Ok(match self { Self::Private(b) => b[r].try_into()?, Self::Shared(b) => { std::array::from_fn(|i| atomic_byte(&b[p + i]).load(Ordering::Relaxed)) } }) } pub fn put(&mut self, p: usize, data: &[u8]) -> Result<()> { let r = self.range(p, data.len())?; match self { Self::Private(b) => b[r].copy_from_slice(data), Self::Shared(b) => { for (v, byte) in b[r].iter().zip(data) { atomic_byte(v).store(*byte, Ordering::Relaxed) } } } Ok(()) } pub fn timestamp(&mut self, p: usize, value: u64) -> Result<()> { let r = self.range(p, 8)?; if let Self::Shared(b) = self { let ptr = b[r.start].get().cast::<u64>(); if ptr.is_aligned() { // Several BSD CPUs can use the same aligned WASI clock output. // A guest load must never see half of each CPU's timestamp. unsafe { AtomicU64::from_ptr(ptr) }.store(value.to_le(), Ordering::SeqCst); return Ok(()); } } self.put(p, &value.to_le_bytes()) } pub fn word(&self, p: usize) -> Result<u32> { match self { Self::Private(_) => Ok(u32::from_le_bytes(self.read(p)?)), Self::Shared(b) => Ok(u32::from_le(atomic_word(b, p)?.load(Ordering::SeqCst))), } } pub fn raise(&mut self, p: usize, bits: u32) -> Result<()> { match self { Self::Private(_) => { let value = self.word(p)? | bits; self.put(p, &value.to_le_bytes())?; } Self::Shared(b) => { atomic_word(b, p)?.fetch_or(bits.to_le(), Ordering::SeqCst); } } Ok(()) }}
fn atomic_byte(v: &UnsafeCell<u8>) -> &AtomicU8 { // Wasmtime's shared-memory API supplies a live, writable byte. AtomicU8 // has byte alignment and every host access to these bytes is atomic. unsafe { AtomicU8::from_ptr(v.get()) }}
fn atomic_word(b: &[UnsafeCell<u8>], p: usize) -> Result<&AtomicU32> { let cells = b .get(p..p.checked_add(4).context("word overflow")?) .context("invalid atomic word")?; let ptr = cells[0].get().cast::<u32>(); ensure!(ptr.is_aligned(), "unaligned IRQ or initialization word"); // These ABI words are reserved for aligned 32-bit atomic accesses. Ok(unsafe { AtomicU32::from_ptr(ptr) })}