#![cfg(all(target_os = "macos", target_arch = "aarch64"))]
use applevisor_sys as av;
use crate::fuse::dax::{HvfMapper, DAX_PROT_READ, DAX_PROT_WRITE};
use crate::fuse::Errno;
const PAGE_SIZE: u64 = 0x4000;
pub struct HvfDaxMapper;
impl HvfDaxMapper {
pub fn new() -> Self {
Self
}
}
impl Default for HvfDaxMapper {
fn default() -> Self {
Self::new()
}
}
fn hvf_to_errno(rc: i32) -> Errno {
match rc as u32 {
0 => 0,
0xfae9_4001 => crate::fuse::backend::EIO,
0xfae9_4002 => -16, 0xfae9_4003 => crate::fuse::backend::EINVAL,
0xfae9_4005 => crate::fuse::backend::ENOSPC,
0xfae9_4006 => -19, _ => crate::fuse::backend::EIO,
}
}
impl HvfMapper for HvfDaxMapper {
fn map(&self, host_va: *mut u8, gpa: u64, len: u64, prot: u32) -> Result<(), Errno> {
if (gpa | len) & (PAGE_SIZE - 1) != 0 {
return Err(crate::fuse::backend::EINVAL);
}
let mut flags: u64 = 0;
if prot & DAX_PROT_READ != 0 {
flags |= av::HV_MEMORY_READ as u64;
}
if prot & DAX_PROT_WRITE != 0 {
flags |= av::HV_MEMORY_WRITE as u64;
}
let rc = unsafe { av::hv_vm_map(host_va as *const _, gpa, len as usize, flags) };
if rc == 0 {
Ok(())
} else {
Err(hvf_to_errno(rc as i32))
}
}
fn unmap(&self, gpa: u64, len: u64) -> Result<(), Errno> {
if (gpa | len) & (PAGE_SIZE - 1) != 0 {
return Err(crate::fuse::backend::EINVAL);
}
let rc = unsafe { av::hv_vm_unmap(gpa, len as usize) };
if rc == 0 {
Ok(())
} else {
Err(hvf_to_errno(rc as i32))
}
}
}