Skip to content

Commit

Permalink
fix(mm): 修复riscv64启动时的PageFault (#915)
Browse files Browse the repository at this point in the history
* 修复riscv64启动时的PageFault

* 优化代码结构
  • Loading branch information
MemoryShore authored Sep 6, 2024
1 parent db7c782 commit a3571c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
16 changes: 9 additions & 7 deletions kernel/src/mm/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,13 +807,15 @@ impl<Arch: MemoryManagementArch> EntryFlags<Arch> {
/// - prot_flags: 页的保护标志
/// - user: 用户空间是否可访问
pub fn from_prot_flags(prot_flags: ProtFlags, user: bool) -> Self {
let vm_flags = super::VmFlags::from(prot_flags);
// let flags: EntryFlags<Arch> = EntryFlags::new()
// .set_user(user)
// .set_execute(prot_flags.contains(ProtFlags::PROT_EXEC))
// .set_write(prot_flags.contains(ProtFlags::PROT_WRITE));
let flags = Arch::vm_get_page_prot(vm_flags).set_user(user);
return flags;
if Arch::PAGE_FAULT_ENABLED {
let vm_flags = super::VmFlags::from(prot_flags);
Arch::vm_get_page_prot(vm_flags).set_user(user)
} else {
EntryFlags::new()
.set_user(user)
.set_execute(prot_flags.contains(ProtFlags::PROT_EXEC))
.set_write(prot_flags.contains(ProtFlags::PROT_WRITE))
}
}

#[inline(always)]
Expand Down
24 changes: 10 additions & 14 deletions kernel/src/mm/ucontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,14 @@ impl InnerAddressSpace {

let len = page_align_up(len);

let vm_flags = VmFlags::from(prot_flags)
| VmFlags::from(map_flags)
| VmFlags::VM_MAYREAD
| VmFlags::VM_MAYWRITE
| VmFlags::VM_MAYEXEC;

// debug!("map_anonymous: len = {}", len);

let start_page: VirtPageFrame = self.mmap(
round_hint_to_min(start_vaddr),
PageFrameCount::from_bytes(len).unwrap(),
prot_flags,
map_flags,
move |page, count, flags, mapper, flusher| {
move |page, count, vm_flags, flags, mapper, flusher| {
if allocate_at_once {
VMA::zeroed(page, count, vm_flags, flags, mapper, flusher, None, None)
} else {
Expand Down Expand Up @@ -359,12 +353,6 @@ impl InnerAddressSpace {

let len = page_align_up(len);

let vm_flags = VmFlags::from(prot_flags)
| VmFlags::from(map_flags)
| VmFlags::VM_MAYREAD
| VmFlags::VM_MAYWRITE
| VmFlags::VM_MAYEXEC;

// debug!("map_anonymous: len = {}", len);

let binding = ProcessManager::current_pcb().fd_table();
Expand All @@ -388,7 +376,7 @@ impl InnerAddressSpace {
PageFrameCount::from_bytes(len).unwrap(),
prot_flags,
map_flags,
move |page, count, flags, mapper, flusher| {
move |page, count, vm_flags, flags, mapper, flusher| {
if allocate_at_once {
VMA::zeroed(
page,
Expand Down Expand Up @@ -436,6 +424,7 @@ impl InnerAddressSpace {
F: FnOnce(
VirtPageFrame,
PageFrameCount,
VmFlags,
EntryFlags<MMArch>,
&mut PageMapper,
&mut dyn Flusher<MMArch>,
Expand Down Expand Up @@ -467,6 +456,12 @@ impl InnerAddressSpace {

let page = VirtPageFrame::new(region.start());

let vm_flags = VmFlags::from(prot_flags)
| VmFlags::from(map_flags)
| VmFlags::VM_MAYREAD
| VmFlags::VM_MAYWRITE
| VmFlags::VM_MAYEXEC;

// debug!("mmap: page: {:?}, region={region:?}", page.virt_address());

compiler_fence(Ordering::SeqCst);
Expand All @@ -483,6 +478,7 @@ impl InnerAddressSpace {
self.mappings.insert_vma(map_func(
page,
page_count,
vm_flags,
EntryFlags::from_prot_flags(prot_flags, true),
&mut self.user_mapper.utable,
flusher,
Expand Down

0 comments on commit a3571c8

Please sign in to comment.