Add support for converting from VmFrame to VmSegment

This commit is contained in:
LI Qing
2024-02-22 14:51:06 +09:00
committed by Tate, Hongliang Tian
parent 437ab804f3
commit e0d92b5203
4 changed files with 59 additions and 33 deletions

View File

@ -10,6 +10,7 @@ bitflags = "1.3"
spin = "0.9.4"
pod = { git = "https://github.com/asterinas/pod", rev = "d7dba56" }
aster-frame = { path = "../../../framework/aster-frame" }
align_ext = { path = "../../../framework/libs/align_ext" }
aster-util = { path = "../../libs/aster-util" }
int-to-c-enum = { path = "../../libs/int-to-c-enum" }
component = { path = "../../libs/comp-sys/component" }

View File

@ -1,5 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use align_ext::AlignExt;
use aster_frame::{
sync::WaitQueue,
vm::{VmFrame, VmReader, VmSegment, VmWriter},
@ -358,29 +359,27 @@ pub enum BioStatus {
#[derive(Debug, Clone)]
pub struct BioSegment {
/// The contiguous pages on which this segment resides.
pages: Pages,
/// The offset (in bytes) relative to the first page.
pages: VmSegment,
/// The starting offset (in bytes) within the first page.
/// The offset should always be aligned to the sector size and
/// must not exceed the size of a single page.
offset: AlignedUsize<SECTOR_SIZE>,
// The length (in bytes), may cross pages.
/// The total length (in bytes).
/// The length can span multiple pages and should be aligned to
/// the sector size.
len: AlignedUsize<SECTOR_SIZE>,
}
const SECTOR_SIZE: u16 = super::SECTOR_SIZE as u16;
#[derive(Debug, Clone)]
enum Pages {
Frame(VmFrame),
Segment(VmSegment),
}
impl<'a> BioSegment {
/// Constructs a new `BioSegment` from `VmSegment`.
pub fn from_segment(segment: VmSegment, offset: usize, len: usize) -> Self {
assert!(offset + len <= segment.nbytes());
Self {
pages: Pages::Segment(segment),
offset: AlignedUsize::<SECTOR_SIZE>::new(offset).unwrap(),
pages: segment.range(frame_range(&(offset..offset + len))),
offset: AlignedUsize::<SECTOR_SIZE>::new(offset % super::BLOCK_SIZE).unwrap(),
len: AlignedUsize::<SECTOR_SIZE>::new(len).unwrap(),
}
}
@ -390,7 +389,7 @@ impl<'a> BioSegment {
assert!(offset + len <= super::BLOCK_SIZE);
Self {
pages: Pages::Frame(frame),
pages: VmSegment::from(frame),
offset: AlignedUsize::<SECTOR_SIZE>::new(offset).unwrap(),
len: AlignedUsize::<SECTOR_SIZE>::new(len).unwrap(),
}
@ -406,25 +405,39 @@ impl<'a> BioSegment {
self.len.value()
}
/// Returns the offset (in bytes) within the first page.
pub fn offset(&self) -> usize {
self.offset.value()
}
/// Returns the contiguous pages on which this segment resides.
pub fn pages(&self) -> &VmSegment {
&self.pages
}
/// Returns a reader to read data from it.
pub fn reader(&'a self) -> VmReader<'a> {
let reader = match &self.pages {
Pages::Segment(segment) => segment.reader(),
Pages::Frame(frame) => frame.reader(),
};
reader.skip(self.offset.value()).limit(self.len.value())
self.pages
.reader()
.skip(self.offset.value())
.limit(self.len.value())
}
/// Returns a writer to write data into it.
pub fn writer(&'a self) -> VmWriter<'a> {
let writer = match &self.pages {
Pages::Segment(segment) => segment.writer(),
Pages::Frame(frame) => frame.writer(),
};
writer.skip(self.offset.value()).limit(self.len.value())
self.pages
.writer()
.skip(self.offset.value())
.limit(self.len.value())
}
}
fn frame_range(byte_range: &Range<usize>) -> Range<usize> {
let start = byte_range.start.align_down(super::BLOCK_SIZE);
let end = byte_range.end.align_up(super::BLOCK_SIZE);
(start / super::BLOCK_SIZE)..(end / super::BLOCK_SIZE)
}
/// An aligned unsigned integer number.
///
/// An instance of `AlignedUsize<const N: u16>` is guaranteed to have a value that is a multiple