Skip to content

Commit

Permalink
fix #13 page fault
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaoqiming committed Jun 7, 2021
1 parent 969ca1b commit 9639e30
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 7 additions & 4 deletions os/src/memory/memory_set/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions os/src/memory/memory_set/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait MemoryHandler: Debug + 'static {
fn box_clone(&self) -> Box<dyn MemoryHandler>;
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<dyn MemoryHandler> {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit 9639e30

Please sign in to comment.