diff --git a/crates/xen/xengnt/Cargo.toml b/crates/xen/xengnt/Cargo.toml index 5d14706..6212de8 100644 --- a/crates/xen/xengnt/Cargo.toml +++ b/crates/xen/xengnt/Cargo.toml @@ -5,10 +5,9 @@ edition = "2021" resolver = "2" [dependencies] -log = { workspace = true } thiserror = { workspace = true } +libc = { workspace = true } nix = { workspace = true, features = ["ioctl"] } -tokio = { workspace = true } [lib] name = "xengnt" diff --git a/crates/xen/xengnt/src/error.rs b/crates/xen/xengnt/src/error.rs index 51926e5..d4932a9 100644 --- a/crates/xen/xengnt/src/error.rs +++ b/crates/xen/xengnt/src/error.rs @@ -8,6 +8,8 @@ pub enum Error { Io(#[from] io::Error), #[error("failed to read structure")] StructureReadFailed, + #[error("mmap failed")] + MmapFailed, } pub type Result = std::result::Result; diff --git a/crates/xen/xengnt/src/lib.rs b/crates/xen/xengnt/src/lib.rs index e858e8c..7998349 100644 --- a/crates/xen/xengnt/src/lib.rs +++ b/crates/xen/xengnt/src/lib.rs @@ -4,15 +4,18 @@ pub mod sys; use error::{Error, Result}; use std::{ fs::{File, OpenOptions}, - os::fd::AsRawFd, + os::{fd::AsRawFd, raw::c_void}, + sync::Arc, }; use sys::{ AllocGref, DeallocGref, GetOffsetForVaddr, GrantRef, MapGrantRef, SetMaxGrants, UnmapGrantRef, UnmapNotify, UNMAP_NOTIFY_CLEAR_BYTE, UNMAP_NOTIFY_SEND_EVENT, }; +use libc::{mmap, munmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE}; + pub struct GrantDevice { - handle: File, + handle: Arc, } impl GrantDevice { @@ -21,17 +24,12 @@ impl GrantDevice { .read(true) .write(true) .open("/dev/xen/gntdev")?; - Ok(GrantDevice { handle }) + Ok(GrantDevice { + handle: Arc::new(handle), + }) } - pub fn map_grant_ref(&self, count: u32) -> Result<(u64, Vec)> { - let refs: Vec = vec![ - GrantRef { - domid: 0, - reference: 0 - }; - count as usize - ]; + pub fn map_grant_ref(&self, refs: Vec) -> Result<(u64, Vec)> { let mut request = MapGrantRef::write(refs.as_slice()); unsafe { sys::map_grant_ref(self.handle.as_raw_fd(), request.as_mut_ptr())?; @@ -91,8 +89,9 @@ impl GrantDevice { } } +#[derive(Clone)] pub struct GrantAlloc { - handle: File, + handle: Arc, } impl GrantAlloc { @@ -101,7 +100,9 @@ impl GrantAlloc { .read(true) .write(true) .open("/dev/xen/gntalloc")?; - Ok(GrantAlloc { handle }) + Ok(GrantAlloc { + handle: Arc::new(handle), + }) } pub fn alloc_gref(&self, domid: u16, flags: u16, count: u32) -> Result<(u64, Vec)> { @@ -140,3 +141,74 @@ impl GrantAlloc { Ok(()) } } + +pub struct GrantTab { + device: GrantDevice, +} + +const PAGE_SIZE: usize = 4096; + +#[allow(clippy::len_without_is_empty)] +pub struct MappedMemory { + length: usize, + addr: *mut c_void, +} + +impl MappedMemory { + pub fn len(&self) -> usize { + self.length + } + + pub fn ptr(&self) -> *mut c_void { + self.addr + } +} + +impl Drop for MappedMemory { + fn drop(&mut self) { + let _ = unsafe { munmap(self.addr, self.length) }; + } +} + +impl GrantTab { + pub fn open() -> Result { + Ok(GrantTab { + device: GrantDevice::open()?, + }) + } + + pub fn map_grant_refs( + &self, + refs: Vec, + read: bool, + write: bool, + ) -> Result { + let (index, refs) = self.device.map_grant_ref(refs)?; + unsafe { + let mut flags: i32 = 0; + if read { + flags |= PROT_READ; + } + + if write { + flags |= PROT_WRITE; + } + + let addr = mmap( + std::ptr::null_mut(), + PAGE_SIZE * refs.len(), + flags, + MAP_SHARED, + self.device.handle.as_raw_fd(), + index as i64, + ); + if addr == MAP_FAILED { + return Err(Error::MmapFailed); + } + Ok(MappedMemory { + addr, + length: PAGE_SIZE * refs.len(), + }) + } + } +}