-
Notifications
You must be signed in to change notification settings - Fork 167
feat(virtq): add packed virtio ring primitives #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andreiltd
wants to merge
1
commit into
hyperlight-dev:main
Choose a base branch
from
andreiltd:tandr/virtq-1-ring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,167 @@ | ||||||
| /* | ||||||
| Copyright 2026 The Hyperlight Authors. | ||||||
|
|
||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
| you may not use this file except in compliance with the License. | ||||||
| You may obtain a copy of the License at | ||||||
|
|
||||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||||
|
|
||||||
| Unless required by applicable law or agreed to in writing, software | ||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
| See the License for the specific language governing permissions and | ||||||
| limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| //! Memory Access Traits for Virtqueue Operations | ||||||
| //! | ||||||
| //! This module defines the [`MemOps`] trait that abstracts memory access patterns | ||||||
| //! required by the virtqueue implementation. This allows the virtqueue code to | ||||||
| //! work with different memory backends e.g. Host vs Guest. | ||||||
|
|
||||||
| use alloc::sync::Arc; | ||||||
|
|
||||||
| use bytemuck::Pod; | ||||||
|
|
||||||
| /// Backend-provided memory access for virtqueue. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// Implementations must ensure that: | ||||||
| /// - Pointers passed to methods are valid for the duration of the call | ||||||
| /// - Memory ordering guarantees are upheld as documented | ||||||
| /// - Reads and writes don't cause undefined behavior (alignment, validity) | ||||||
| /// | ||||||
| /// [`RingProducer`]: super::RingProducer | ||||||
| /// [`RingConsumer`]: super::RingConsumer | ||||||
| pub trait MemOps { | ||||||
| type Error; | ||||||
|
|
||||||
| /// Read bytes from physical memory. | ||||||
| /// | ||||||
| /// Used for reading buffer contents pointed to by descriptors. | ||||||
| /// | ||||||
| /// # Arguments | ||||||
| /// | ||||||
| /// * `addr` - Guest physical address to read from | ||||||
| /// * `dst` - Destination buffer to fill | ||||||
| /// | ||||||
| /// # Returns | ||||||
| /// | ||||||
| /// Number of bytes actually read (should equal `dst.len()` on success). | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// The caller must ensure `paddr` is valid and points to at least `dst.len()` bytes. | ||||||
| fn read(&self, addr: u64, dst: &mut [u8]) -> Result<usize, Self::Error>; | ||||||
|
|
||||||
| /// Write bytes to physical memory. | ||||||
| /// | ||||||
| /// # Arguments | ||||||
| /// | ||||||
| /// * `addr` - address to write to | ||||||
| /// * `src` - Source data to write | ||||||
| /// | ||||||
| /// # Returns | ||||||
| /// | ||||||
| /// Number of bytes actually written (should equal `src.len()` on success). | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// The caller must ensure `paddr` is valid and points to at least `src.len()` bytes. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Suggested change
|
||||||
| fn write(&self, addr: u64, src: &[u8]) -> Result<usize, Self::Error>; | ||||||
|
|
||||||
| /// Load a u16 with acquire semantics. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// `addr` must translate to a valid, aligned `AtomicU16` in shared memory. | ||||||
| fn load_acquire(&self, addr: u64) -> Result<u16, Self::Error>; | ||||||
|
|
||||||
| /// Store a u16 with release semantics. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// `addr` must translate to a valid `AtomicU16` in shared memory. | ||||||
| fn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error>; | ||||||
|
|
||||||
| /// Get a direct read-only slice into shared memory. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// The caller must ensure: | ||||||
| /// - `addr` is valid and points to at least `len` bytes. | ||||||
| /// - The memory region is not concurrently modified for the lifetime of | ||||||
| /// the returned slice. Caller must uphold this via protocol-level | ||||||
| /// synchronisation, e.g. descriptor ownership transfer. | ||||||
| /// | ||||||
| /// See also [`BufferOwner`]: super::BufferOwner | ||||||
| unsafe fn as_slice(&self, addr: u64, len: usize) -> Result<&[u8], Self::Error>; | ||||||
|
|
||||||
| /// Get a direct mutable slice into shared memory. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// The caller must ensure: | ||||||
| /// - `addr` is valid and points to at least `len` bytes. | ||||||
| /// - No other references (shared or mutable) to this memory region exist | ||||||
| /// for the lifetime of the returned slice. | ||||||
| /// - Protocol-level synchronisation (e.g. descriptor ownership) guarantees | ||||||
| /// exclusive access. | ||||||
| #[allow(clippy::mut_from_ref)] | ||||||
| unsafe fn as_mut_slice(&self, addr: u64, len: usize) -> Result<&mut [u8], Self::Error>; | ||||||
|
|
||||||
| /// Read a Pod type at the given pointer. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// The caller must ensure `addr` is valid, aligned, and translates to initialized memory. | ||||||
| fn read_val<T: Pod>(&self, addr: u64) -> Result<T, Self::Error> { | ||||||
| let mut val = T::zeroed(); | ||||||
| let bytes = bytemuck::bytes_of_mut(&mut val); | ||||||
|
|
||||||
| self.read(addr, bytes)?; | ||||||
| Ok(val) | ||||||
| } | ||||||
|
|
||||||
| /// Write a Pod type at the given pointer. | ||||||
| /// | ||||||
| /// # Safety | ||||||
| /// | ||||||
| /// The caller ensures that `ptr` is valid. | ||||||
| fn write_val<T: Pod>(&self, addr: u64, val: T) -> Result<(), Self::Error> { | ||||||
| let bytes = bytemuck::bytes_of(&val); | ||||||
| self.write(addr, bytes)?; | ||||||
| Ok(()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl<T: MemOps> MemOps for Arc<T> { | ||||||
| type Error = T::Error; | ||||||
|
|
||||||
| fn read(&self, addr: u64, dst: &mut [u8]) -> Result<usize, Self::Error> { | ||||||
| (**self).read(addr, dst) | ||||||
| } | ||||||
|
|
||||||
| fn write(&self, addr: u64, src: &[u8]) -> Result<usize, Self::Error> { | ||||||
| (**self).write(addr, src) | ||||||
| } | ||||||
|
|
||||||
| fn load_acquire(&self, addr: u64) -> Result<u16, Self::Error> { | ||||||
| (**self).load_acquire(addr) | ||||||
| } | ||||||
|
|
||||||
| fn store_release(&self, addr: u64, val: u16) -> Result<(), Self::Error> { | ||||||
| (**self).store_release(addr, val) | ||||||
| } | ||||||
|
|
||||||
| unsafe fn as_slice(&self, addr: u64, len: usize) -> Result<&[u8], Self::Error> { | ||||||
| unsafe { (**self).as_slice(addr, len) } | ||||||
| } | ||||||
|
|
||||||
| #[allow(clippy::mut_from_ref)] | ||||||
| unsafe fn as_mut_slice(&self, addr: u64, len: usize) -> Result<&mut [u8], Self::Error> { | ||||||
| unsafe { (**self).as_mut_slice(addr, len) } | ||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: