Refactor VmReader/Writer to support both kernel space and user space

This commit is contained in:
Chen Chengjun
2024-06-28 09:45:51 +08:00
committed by Tate, Hongliang Tian
parent 994427c71a
commit 09b6153d29
5 changed files with 398 additions and 133 deletions

View File

@ -114,14 +114,20 @@ impl HasPaddr for Frame {
impl<'a> Frame {
/// Returns a reader to read data from it.
pub fn reader(&'a self) -> VmReader<'a> {
// SAFETY: the memory of the page is contiguous and is valid during `'a`.
unsafe { VmReader::from_raw_parts(self.as_ptr(), self.size()) }
// SAFETY: the memory of the page is untyped, contiguous and is valid during `'a`.
// Currently, only slice can generate `VmWriter` with typed memory, and this `Frame` cannot
// generate or be generated from an alias slice, so the reader will not overlap with `VmWriter`
// with typed memory.
unsafe { VmReader::from_kernel_space(self.as_ptr(), self.size()) }
}
/// Returns a writer to write data into it.
pub fn writer(&'a self) -> VmWriter<'a> {
// SAFETY: the memory of the page is contiguous and is valid during `'a`.
unsafe { VmWriter::from_raw_parts_mut(self.as_mut_ptr(), self.size()) }
// SAFETY: the memory of the page is untyped, contiguous and is valid during `'a`.
// Currently, only slice can generate `VmReader` with typed memory, and this `Frame` cannot
// generate or be generated from an alias slice, so the writer will not overlap with `VmReader`
// with typed memory.
unsafe { VmWriter::from_kernel_space(self.as_mut_ptr(), self.size()) }
}
}