Skip to content

Commit

Permalink
[feat] Add axns for task
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-stars committed Jan 1, 2025
1 parent 0b59da2 commit 86c8ddf
Show file tree
Hide file tree
Showing 9 changed files with 465 additions and 21 deletions.
392 changes: 392 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ linkme = "0.3"
axerrno = "0.1"
memory_addr = "0.3"
xmas-elf = "0.9"
crate_interface = "0.1"
bitflags = "2.6"
kernel-elf-parser = "0.1.0"
num_enum = { version = "0.7", default-features = false }
syscalls = { version = "0.6", default-features = false }

axstd = { git = "https://github.com/arceos-org/arceos.git", features = ["paging"] }
axhal = { git = "https://github.com/arceos-org/arceos.git", features = ["uspace"] }
axmm = { git = "https://github.com/arceos-org/arceos.git" }
axtask = { git = "https://github.com/arceos-org/arceos.git" }
axsync = { git = "https://github.com/arceos-org/arceos.git" }
axruntime = { git = "https://github.com/arceos-org/arceos.git", features = ["multitask"] }
arceos_posix_api = { git = "https://github.com/arceos-org/arceos.git" }
axstd = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic", features = ["paging"] }
axhal = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic", features = ["uspace"] }
axmm = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic" }
axtask = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic" }
axsync = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic" }
axruntime = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic", features = ["multitask"] }
arceos_posix_api = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic", features = ["uspace"] }
axns = { git = "https://github.com/arceos-org/arceos.git", branch = "monolithic", features = ["thread-local"] }

[target.'cfg(target_arch = "x86_64")'.dependencies]
x86 = "0.52"
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ all: build

ax_root:
@./scripts/set_ax_root.sh $(AX_ROOT)
@make -C $(AX_ROOT) disk_img

user_apps:
@make -C ./apps/$(AX_TESTCASE) ARCH=$(ARCH) build
Expand All @@ -23,7 +24,7 @@ test:
@./scripts/app_test.sh

build run justrun debug disasm: ax_root
@make -C $(AX_ROOT) A=$(PWD) FEATURES=$(FEATURES) $@
@make -C $(AX_ROOT) A=$(PWD) FEATURES=$(FEATURES) BLK=y NET=y $@

clean: ax_root
@make -C $(AX_ROOT) A=$(PWD) clean
Expand Down
12 changes: 6 additions & 6 deletions apps/nimbos/expect_off.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ log_level = off

Hello world from user mode program!
Hello, world!
Hello, I am process 2.
Back in process 2, iteration 0.
Back in process 2, iteration 1.
Back in process 2, iteration 2.
Back in process 2, iteration 3.
Back in process 2, iteration 4.
Hello, I am process 6.
Back in process 6, iteration 0.
Back in process 6, iteration 1.
Back in process 6, iteration 2.
Back in process 6, iteration 3.
Back in process 6, iteration 4.
yield passed!
into sleep test!
simple_sleep passed!
1 change: 1 addition & 0 deletions scripts/config.toml.temp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ axstd = { path = "%AX_ROOT%/ulib/axstd" }
arceos_posix_api = { path = "%AX_ROOT%/api/arceos_posix_api" }
axhal = { path = "%AX_ROOT%/modules/axhal" }
axmm = { path = "%AX_ROOT%/modules/axmm" }
axns = { path = "%AX_ROOT%/modules/axns" }
axtask = { path = "%AX_ROOT%/modules/axtask" }
axsync = { path = "%AX_ROOT%/modules/axsync" }
axruntime = { path = "%AX_ROOT%/modules/axruntime" }
2 changes: 1 addition & 1 deletion scripts/get_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
AX_ROOT=.arceos

test ! -d "$AX_ROOT" && echo "Cloning repositories ..." || true
test ! -d "$AX_ROOT" && git clone https://github.com/arceos-org/arceos -b monolithic "$AX_ROOT" --depth=1 || true
test ! -d "$AX_ROOT" && git clone https://github.com/Azure-stars/Starry -b monolithic "$AX_ROOT" --depth=1 || true

$(dirname $0)/set_ax_root.sh $AX_ROOT
6 changes: 6 additions & 0 deletions src/syscall_imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ fn handle_syscall(tf: &TrapFrame, syscall_num: usize) -> isize {
Sysno::writev => sys_writev(tf.arg0() as _, tf.arg1() as _, tf.arg2() as _),
Sysno::sched_yield => sys_sched_yield() as isize,
Sysno::nanosleep => sys_nanosleep(tf.arg0() as _, tf.arg1() as _) as _,
Sysno::clock_nanosleep => sys_clock_nanosleep(
tf.arg0() as _,
tf.arg1() as _,
tf.arg2() as _,
tf.arg3() as _,
) as _,
Sysno::getpid => sys_getpid() as isize,
Sysno::exit => sys_exit(tf.arg0() as _),
#[cfg(target_arch = "x86_64")]
Expand Down
30 changes: 26 additions & 4 deletions src/syscall_imp/task/schedule.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
use arceos_posix_api as api;
use arceos_posix_api::{
self as api,
ctypes::{clockid_t, timespec},
};
use axerrno::LinuxError;

pub(crate) fn sys_sched_yield() -> i32 {
api::sys_sched_yield()
}

pub(crate) fn sys_nanosleep(
req: *const api::ctypes::timespec,
rem: *mut api::ctypes::timespec,
pub(crate) fn sys_nanosleep(req: *const timespec, rem: *mut timespec) -> i32 {
unsafe { api::sys_nanosleep(req, rem) }
}

pub(crate) fn sys_clock_nanosleep(
clock_id: clockid_t,
flags: isize,
req: *const timespec,
rem: *mut timespec,
) -> i32 {
// CLOCK defaults to CLOCK_REALTIME
// flags defaults to 0

if clock_id != api::ctypes::CLOCK_REALTIME as clockid_t {
// For older linux headers, it does not define ENOTSUP, so we use EOPNOTSUPP instead
return -LinuxError::EOPNOTSUPP.code();
}

if flags != 0 {
return -LinuxError::EOPNOTSUPP.code();
}

unsafe { api::sys_nanosleep(req, rem) }
}
24 changes: 22 additions & 2 deletions src/task.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use alloc::sync::Arc;
use core::sync::atomic::AtomicU64;

use alloc::sync::Arc;

use axhal::arch::UspaceContext;
use axmm::AddrSpace;
use axns::{AxNamespace, AxNamespaceIf};
use axsync::Mutex;
use axtask::{AxTaskRef, TaskExtRef, TaskInner};

Expand All @@ -20,15 +22,18 @@ pub struct TaskExt {
pub uctx: UspaceContext,
/// The virtual memory address space.
pub aspace: Arc<Mutex<AddrSpace>>,
/// The resource namespace
pub ns: AxNamespace,
}

impl TaskExt {
pub const fn new(uctx: UspaceContext, aspace: Arc<Mutex<AddrSpace>>) -> Self {
pub fn new(uctx: UspaceContext, aspace: Arc<Mutex<AddrSpace>>) -> Self {
Self {
proc_id: 233,
uctx,
clear_child_tid: AtomicU64::new(0),
aspace,
ns: AxNamespace::new_thread_local(),
}
}

Expand All @@ -43,6 +48,21 @@ impl TaskExt {
}
}

struct AxNamespaceImpl;

#[crate_interface::impl_interface]
impl AxNamespaceIf for AxNamespaceImpl {
#[inline(never)]
fn current_namespace_base() -> *mut u8 {
let current = axtask::current();
// Safety: We only check whether the task extended data is null and do not access it.
if unsafe { current.task_ext_ptr() }.is_null() {
return axns::AxNamespace::global().base();
}
current.task_ext().ns.base()
}
}

axtask::def_task_ext!(TaskExt);

pub fn spawn_user_task(aspace: Arc<Mutex<AddrSpace>>, uctx: UspaceContext) -> AxTaskRef {
Expand Down

0 comments on commit 86c8ddf

Please sign in to comment.