Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit 5603008

Browse files
authored
Adapt to changes to AddrSpace in ArceOS (#17)
* feat: adapt to changes to AddrSpace in arceos * style: format * fix: add patch to page_table_multiarch * trigger rerun * fix: forked aspace does not copy from kernel
1 parent 8194944 commit 5603008

4 files changed

Lines changed: 50 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ axns = { git = "https://github.com/oscomp/arceos.git", features = ["thread-local
4040

4141
[patch.crates-io]
4242
syscalls = { git = "https://github.com/jasonwhite/syscalls.git", rev = "92624de"}
43+
page_table_multiarch = { git = "https://github.com/Mivik/page_table_multiarch.git", rev = "19ededd" }
44+
page_table_entry = { git = "https://github.com/Mivik/page_table_multiarch.git", rev = "19ededd" }
4345

4446
[target.'cfg(target_arch = "x86_64")'.dependencies]
4547
x86 = "0.52"

src/main.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,38 @@ use alloc::{
1818
sync::Arc,
1919
vec::Vec,
2020
};
21+
use axerrno::AxResult;
2122
use axhal::arch::UspaceContext;
23+
use axmm::{AddrSpace, kernel_aspace};
2224
use axsync::Mutex;
2325
use memory_addr::VirtAddr;
2426

25-
fn run_user_app(args: &[String], envs: &[String]) -> Option<i32> {
26-
let mut uspace = axmm::new_user_aspace(
27+
fn new_user_aspace_empty() -> AxResult<AddrSpace> {
28+
AddrSpace::new_empty(
2729
VirtAddr::from_usize(axconfig::plat::USER_SPACE_BASE),
2830
axconfig::plat::USER_SPACE_SIZE,
2931
)
30-
.expect("Failed to create user address space");
32+
}
33+
34+
/// If the target architecture requires it, the kernel portion of the address
35+
/// space will be copied to the user address space.
36+
fn copy_from_kernel(aspace: &mut AddrSpace) -> AxResult {
37+
if !cfg!(target_arch = "aarch64") && !cfg!(target_arch = "loongarch64") {
38+
// ARMv8 (aarch64) and LoongArch64 use separate page tables for user space
39+
// (aarch64: TTBR0_EL1, LoongArch64: PGDL), so there is no need to copy the
40+
// kernel portion to the user page table.
41+
aspace.copy_mappings_from(&kernel_aspace().lock())?;
42+
}
43+
Ok(())
44+
}
45+
46+
fn run_user_app(args: &[String], envs: &[String]) -> Option<i32> {
47+
let mut uspace = new_user_aspace_empty()
48+
.and_then(|mut it| {
49+
copy_from_kernel(&mut it)?;
50+
Ok(it)
51+
})
52+
.expect("Failed to create user address space");
3153

3254
let path = arceos_posix_api::FilePath::new(&args[0]).expect("Invalid file path");
3355
axfs::api::set_current_dir(path.parent().unwrap()).expect("Failed to set current dir");

src/task.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ use core::{
1111
cell::UnsafeCell,
1212
sync::atomic::{AtomicU64, Ordering},
1313
};
14+
use memory_addr::VirtAddrRange;
1415
use spin::Once;
1516

16-
use crate::ctypes::{CloneFlags, TimeStat, WaitStatus};
17+
use crate::{
18+
copy_from_kernel,
19+
ctypes::{CloneFlags, TimeStat, WaitStatus},
20+
};
1721
use axhal::{
1822
arch::{TrapFrame, UspaceContext},
1923
time::{NANOS_PER_MICROS, NANOS_PER_SEC, monotonic_time_nanos},
2024
};
21-
use axmm::AddrSpace;
25+
use axmm::{AddrSpace, kernel_aspace};
2226
use axns::{AxNamespace, AxNamespaceIf};
2327
use axsync::Mutex;
2428
use axtask::{AxTaskRef, TaskExtRef, TaskInner, current};
@@ -100,7 +104,8 @@ impl TaskExt {
100104

101105
let current_task = current();
102106
let mut current_aspace = current_task.task_ext().aspace.lock();
103-
let new_aspace = current_aspace.clone_or_err()?;
107+
let mut new_aspace = current_aspace.clone_or_err()?;
108+
copy_from_kernel(&mut new_aspace)?;
104109
new_task
105110
.ctx_mut()
106111
.set_page_table_root(new_aspace.page_table_root());
@@ -219,6 +224,18 @@ impl AxNamespaceIf for AxNamespaceImpl {
219224
}
220225
}
221226

227+
impl Drop for TaskExt {
228+
fn drop(&mut self) {
229+
if !cfg!(target_arch = "aarch64") && !cfg!(target_arch = "loongarch64") {
230+
// See [`crate::new_user_aspace`]
231+
let kernel = kernel_aspace().lock();
232+
self.aspace
233+
.lock()
234+
.clear_mappings(VirtAddrRange::from_start_size(kernel.base(), kernel.size()));
235+
}
236+
}
237+
}
238+
222239
axtask::def_task_ext!(TaskExt);
223240

224241
pub fn spawn_user_task(

0 commit comments

Comments
 (0)