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

136 lines
3.5 KiB
Rust
Raw Normal View History

2023-08-07 20:03:15 -05:00
//! Block memory copier state machine
2023-07-25 19:04:26 -05:00
use {
super::{mem::MemoryAccessReason, Memory, VmRunError},
crate::mem::Address,
2023-07-25 19:04:26 -05:00
core::{mem::MaybeUninit, task::Poll},
};
2023-08-07 20:03:15 -05:00
/// Buffer size (defaults to 4 KiB, a smallest page size on most platforms)
2023-07-25 19:04:26 -05:00
const BUF_SIZE: usize = 4096;
2023-08-07 20:03:15 -05:00
/// Buffer of possibly uninitialised bytes, aligned to [`BUF_SIZE`]
2023-07-25 19:04:26 -05:00
#[repr(align(4096))]
struct AlignedBuf([MaybeUninit<u8>; BUF_SIZE]);
2023-08-07 20:03:15 -05:00
/// State for block memory copy
2023-07-25 19:04:26 -05:00
pub struct BlockCopier {
/// Source address
src: Address,
2023-07-25 19:04:26 -05:00
/// Destination address
dst: Address,
2023-07-25 19:04:26 -05:00
/// How many buffer sizes to copy?
n_buffers: usize,
/// …and what remainds after?
rem: usize,
}
impl BlockCopier {
2023-08-07 20:03:15 -05:00
/// Construct a new one
2023-07-26 13:54:24 -05:00
#[inline]
pub fn new(src: Address, dst: Address, count: usize) -> Self {
2023-07-25 19:04:26 -05:00
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
self.src += BUF_SIZE;
self.dst += BUF_SIZE;
2023-07-25 19:28:14 -05:00
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(()))
}
}
2023-08-07 20:03:15 -05:00
/// Load to buffer and store from buffer
2023-07-25 19:04:26 -05:00
#[inline]
unsafe fn act(
2023-08-07 19:48:47 -05:00
memory: &mut impl Memory,
src: Address,
dst: Address,
2023-07-25 19:04:26 -05:00
buf: *mut u8,
count: usize,
) -> Result<(), BlkCopyError> {
// Load to buffer
memory
2023-08-07 19:48:47 -05:00
.load(src, buf, count)
.map_err(|super::mem::LoadError(addr)| BlkCopyError {
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)
.map_err(|super::mem::StoreError(addr)| BlkCopyError {
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)]
pub struct BlkCopyError {
/// Kind of access
access_reason: MemoryAccessReason,
/// VM Address
addr: Address,
2023-07-25 19:04:26 -05:00
}
impl From<BlkCopyError> for VmRunError {
fn from(value: BlkCopyError) -> Self {
match value.access_reason {
MemoryAccessReason::Load => Self::LoadAccessEx(value.addr),
MemoryAccessReason::Store => Self::StoreAccessEx(value.addr),
2023-07-25 19:04:26 -05:00
}
}
}