Implement copy for VmFrame

This commit is contained in:
Jianfeng Jiang
2023-07-20 15:32:50 +08:00
committed by Tate, Hongliang Tian
parent 38ee2cafcd
commit 3a3cb7cd15
2 changed files with 20 additions and 2 deletions

View File

@ -359,6 +359,24 @@ impl VmFrame {
PAGE_SIZE,
)
}
pub fn as_ptr(&self) -> *const u8 {
super::paddr_to_vaddr(self.start_paddr()) as *const u8
}
pub fn as_mut_ptr(&self) -> *mut u8 {
super::paddr_to_vaddr(self.start_paddr()) as *mut u8
}
pub fn copy_from_frame(&self, src: &VmFrame) {
if Arc::ptr_eq(&self.frame_index, &src.frame_index) {
return;
}
// Safety: src and dst is not overlapped.
unsafe {
core::ptr::copy_nonoverlapping(src.as_ptr(), self.as_mut_ptr(), PAGE_SIZE);
}
}
}
impl VmIo for VmFrame {

View File

@ -216,12 +216,12 @@ impl VmoInner {
if self.should_share_frame_with_parent(write_page) {
return Ok(inherited_frame);
}
let frame = {
let options = VmAllocOptions::new(1);
VmFrameVec::allocate(&options)?.pop().unwrap()
};
frame.write_bytes(0, &*tmp_buffer)?;
frame.copy_from_frame(&inherited_frame);
Ok(frame)
}