1
0
Fork 0
forked from koniifer/ableos
ableos-framebuffer/hbvm/src/mem/bmc.rs

152 lines
4 KiB
Rust
Raw Normal View History

2023-07-25 19:04:26 -05:00
use {
super::MemoryAccessReason,
2023-08-07 19:48:47 -05:00
crate::{mem::Memory, VmRunError},
2023-07-25 19:04:26 -05:00
core::{mem::MaybeUninit, task::Poll},
};
// Buffer size (defaults to 4 KiB, a smallest page size on most platforms)
const BUF_SIZE: usize = 4096;
// This should be equal to `BUF_SIZE`
#[repr(align(4096))]
struct AlignedBuf([MaybeUninit<u8>; BUF_SIZE]);
pub struct BlockCopier {
/// Source address
src: u64,
/// Destination address
dst: u64,
/// How many buffer sizes to copy?
n_buffers: usize,
/// …and what remainds after?
rem: usize,
}
impl BlockCopier {
2023-07-26 13:54:24 -05:00
#[inline]
2023-07-25 19:04:26 -05:00
pub fn new(src: u64, dst: u64, count: usize) -> Self {
Self {
src,
dst,
n_buffers: count / BUF_SIZE,
rem: count % BUF_SIZE,
}
}
/// Copy one block
2023-07-25 19:28:14 -05:00
///
2023-07-25 19:04:26 -05:00
/// # Safety
/// - Same as for [`Memory::load`] and [`Memory::store`]
2023-08-07 19:48:47 -05:00
pub unsafe fn poll(&mut self, memory: &mut impl Memory) -> Poll<Result<(), BlkCopyError>> {
2023-07-25 19:04:26 -05:00
// Safety: Assuming uninit of array of MaybeUninit is sound
let mut buf = AlignedBuf(MaybeUninit::uninit().assume_init());
2023-07-26 13:49:23 -05:00
// We have at least one buffer size to copy
2023-07-25 19:04:26 -05:00
if self.n_buffers != 0 {
if let Err(e) = act(
memory,
self.src,
self.dst,
buf.0.as_mut_ptr().cast(),
BUF_SIZE,
) {
return Poll::Ready(Err(e));
}
2023-07-26 13:49:23 -05:00
// Bump source and destination address
//
// If we are over the address space, bail.
2023-07-25 19:28:14 -05:00
match self.src.checked_add(BUF_SIZE as u64) {
Some(n) => self.src = n,
None => return Poll::Ready(Err(BlkCopyError::OutOfBounds)),
};
2023-08-07 19:06:15 -05:00
2023-07-25 19:28:14 -05:00
match self.dst.checked_add(BUF_SIZE as u64) {
Some(n) => self.dst = n,
None => return Poll::Ready(Err(BlkCopyError::OutOfBounds)),
};
2023-07-25 19:04:26 -05:00
self.n_buffers -= 1;
return if self.n_buffers + self.rem == 0 {
// If there is nothing left, we are done
Poll::Ready(Ok(()))
} else {
// Otherwise let's advice to run it again
Poll::Pending
};
}
if self.rem != 0 {
if let Err(e) = act(
memory,
self.src,
self.dst,
buf.0.as_mut_ptr().cast(),
self.rem,
) {
return Poll::Ready(Err(e));
}
}
Poll::Ready(Ok(()))
}
}
#[inline]
unsafe fn act(
2023-08-07 19:48:47 -05:00
memory: &mut impl Memory,
2023-07-25 19:04:26 -05:00
src: u64,
dst: u64,
buf: *mut u8,
count: usize,
) -> Result<(), BlkCopyError> {
// Load to buffer
memory
2023-08-07 19:48:47 -05:00
.load(src, buf, count)
2023-08-07 19:06:15 -05:00
.map_err(|super::LoadError(addr)| BlkCopyError::Access {
2023-07-25 19:04:26 -05:00
access_reason: MemoryAccessReason::Load,
addr,
})?;
// Store from buffer
memory
2023-08-07 19:48:47 -05:00
.store(dst, buf, count)
2023-08-07 19:06:15 -05:00
.map_err(|super::StoreError(addr)| BlkCopyError::Access {
2023-07-25 19:04:26 -05:00
access_reason: MemoryAccessReason::Store,
addr,
})?;
Ok(())
}
/// Error occured when copying a block of memory
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2023-07-25 19:28:14 -05:00
pub enum BlkCopyError {
/// Memory access error
Access {
/// Kind of access
access_reason: MemoryAccessReason,
/// VM Address
addr: u64,
},
/// Address out of bounds
OutOfBounds,
2023-07-25 19:04:26 -05:00
}
impl From<BlkCopyError> for VmRunError {
fn from(value: BlkCopyError) -> Self {
2023-07-25 19:28:14 -05:00
match value {
BlkCopyError::Access {
access_reason: MemoryAccessReason::Load,
addr,
} => Self::LoadAccessEx(addr),
BlkCopyError::Access {
access_reason: MemoryAccessReason::Store,
addr,
} => Self::StoreAccessEx(addr),
BlkCopyError::OutOfBounds => Self::AddrOutOfBounds,
2023-07-25 19:04:26 -05:00
}
}
}