-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add page tfault handler to support lazy mapping
- Loading branch information
1 parent
beef4f2
commit 3c8dfce
Showing
4 changed files
with
82 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use axerrno::AxResult; | ||
use memory_addr::VirtAddr; | ||
|
||
use axhal::mem::virt_to_phys; | ||
use axhal::paging::MappingFlags; | ||
use axhal::trap::{register_trap_handler, PAGE_FAULT}; | ||
use axmm::AddrSpace; | ||
use axtask::TaskExtRef; | ||
|
||
pub fn load_user_app(entry_fn: fn(usize)) -> AxResult<(VirtAddr, VirtAddr, AddrSpace)> { | ||
let entry_fn_kvaddr = VirtAddr::from(entry_fn as usize); | ||
let load_vaddr = VirtAddr::from(0x1000); | ||
let load_paddr = virt_to_phys(entry_fn_kvaddr.align_down_4k()); | ||
let entry_vaddr = load_vaddr + entry_fn_kvaddr.align_offset_4k(); | ||
|
||
let mut uspace = axmm::new_user_aspace()?; | ||
let ustack_top = uspace.end(); | ||
let ustack_vaddr = ustack_top - crate::USER_STACK_SIZE; | ||
uspace.map_linear( | ||
load_vaddr, | ||
load_paddr, | ||
4096, | ||
MappingFlags::READ | MappingFlags::EXECUTE | MappingFlags::USER, | ||
)?; | ||
uspace.map_alloc( | ||
ustack_vaddr, | ||
crate::USER_STACK_SIZE, | ||
MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER, | ||
false, | ||
)?; | ||
info!("New user address space: {:#x?}", uspace); | ||
Ok((entry_vaddr, ustack_top, uspace)) | ||
} | ||
|
||
#[register_trap_handler(PAGE_FAULT)] | ||
fn handle_page_fault(vaddr: VirtAddr, access_flags: MappingFlags, is_user: bool) -> bool { | ||
if is_user { | ||
if !axtask::current() | ||
.task_ext() | ||
.aspace | ||
.lock() | ||
.handle_page_fault(vaddr, access_flags) | ||
{ | ||
warn!("{}: segmentation fault, exit!", axtask::current().id_name()); | ||
axtask::exit(-1); | ||
} | ||
true | ||
} else { | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters