diff --git a/os/src/memory/memory_set/area.rs b/os/src/memory/memory_set/area.rs index 6d36f52..705e340 100644 --- a/os/src/memory/memory_set/area.rs +++ b/os/src/memory/memory_set/area.rs @@ -50,12 +50,15 @@ impl MemoryArea { pub fn page_copy(&self, pt: &mut PageTableImpl, src: usize, length: usize) { let mut l = length; let mut s = src; + let mut offset = self.start % PAGE_SIZE; for page in PageRange::new(self.start, self.end) { + let copy_size = PAGE_SIZE-offset; self.handler - .page_copy(pt, page, s, if l < PAGE_SIZE { l } else { PAGE_SIZE }); - s += PAGE_SIZE; - if l >= PAGE_SIZE { - l -= PAGE_SIZE; + .page_copy(pt, page, offset, s, if l < copy_size { l } else { copy_size }); + offset = 0; + s += copy_size; + if l >= copy_size { + l -= copy_size; } } } diff --git a/os/src/memory/memory_set/handler.rs b/os/src/memory/memory_set/handler.rs index 76e1577..b2044c6 100644 --- a/os/src/memory/memory_set/handler.rs +++ b/os/src/memory/memory_set/handler.rs @@ -10,7 +10,7 @@ pub trait MemoryHandler: Debug + 'static { fn box_clone(&self) -> Box; fn map(&self, pt: &mut PageTableImpl, va: usize, attr: &MemoryAttr); fn unmap(&self, pt: &mut PageTableImpl, va: usize); - fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize); + fn page_copy(&self, pt: &mut PageTableImpl, va: usize, va_offset: usize, src: usize, length: usize); } impl Clone for Box { @@ -39,12 +39,12 @@ impl MemoryHandler for Linear { fn unmap(&self, pt: &mut PageTableImpl, va: usize) { pt.unmap(va); } - fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize) { + fn page_copy(&self, pt: &mut PageTableImpl, va: usize, va_offset: usize, src: usize, length: usize) { let pa = pt.get_entry(va).expect("get pa error!").0.addr().as_usize(); assert!(va == access_pa_via_va(pa)); assert!(va == pa + self.offset); unsafe { - let dst = core::slice::from_raw_parts_mut(va as *mut u8, PAGE_SIZE); + let dst = core::slice::from_raw_parts_mut((va+va_offset) as *mut u8, PAGE_SIZE); if length > 0 { let src = core::slice::from_raw_parts(src as *const u8, PAGE_SIZE); dst[..length].clone_from_slice(&src[..length]); @@ -78,10 +78,10 @@ impl MemoryHandler for ByFrame { fn unmap(&self, pt: &mut PageTableImpl, va: usize) { pt.unmap(va); } - fn page_copy(&self, pt: &mut PageTableImpl, va: usize, src: usize, length: usize) { + fn page_copy(&self, pt: &mut PageTableImpl, va: usize, va_offset: usize, src: usize, length: usize) { let pa = pt.get_entry(va).expect("get pa error!").0.addr().as_usize(); unsafe { - let dst = core::slice::from_raw_parts_mut(access_pa_via_va(pa) as *mut u8, PAGE_SIZE); + let dst = core::slice::from_raw_parts_mut((access_pa_via_va(pa)+va_offset) as *mut u8, PAGE_SIZE); if length > 0 { let src = core::slice::from_raw_parts(src as *const u8, PAGE_SIZE); dst[..length].clone_from_slice(&src[..length]);