From 693ea9264264c5416809e0f0e8aac48f34112fae Mon Sep 17 00:00:00 2001 From: Youjie Zheng Date: Wed, 11 Dec 2024 20:51:45 +0800 Subject: [PATCH] [feat] Add axns for task --- Cargo.toml | 4 +++- Makefile | 3 ++- apps/nimbos/expect_off.out | 12 ++++++------ scripts/config.toml.temp | 1 + scripts/get_deps.sh | 2 +- src/syscall_imp/mod.rs | 6 ++++++ src/syscall_imp/task/schedule.rs | 30 ++++++++++++++++++++++++++---- src/task.rs | 24 ++++++++++++++++++++++-- 8 files changed, 67 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 454b5c4f..ab888645 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ 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 } @@ -23,7 +24,8 @@ 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" } +arceos_posix_api = { git = "https://github.com/arceos-org/arceos.git", features = ["uspace"] } +axns = { git = "https://github.com/arceos-org/arceos.git", features = ["thread-local"] } [target.'cfg(target_arch = "x86_64")'.dependencies] x86 = "0.52" diff --git a/Makefile b/Makefile index 096f6568..312ced81 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/apps/nimbos/expect_off.out b/apps/nimbos/expect_off.out index 1f2e72e6..fdb8f893 100644 --- a/apps/nimbos/expect_off.out +++ b/apps/nimbos/expect_off.out @@ -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! \ No newline at end of file diff --git a/scripts/config.toml.temp b/scripts/config.toml.temp index 9211c0ca..790a7a82 100644 --- a/scripts/config.toml.temp +++ b/scripts/config.toml.temp @@ -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" } diff --git a/scripts/get_deps.sh b/scripts/get_deps.sh index 92011146..2ced3f39 100755 --- a/scripts/get_deps.sh +++ b/scripts/get_deps.sh @@ -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 diff --git a/src/syscall_imp/mod.rs b/src/syscall_imp/mod.rs index 3517fca4..61da5508 100644 --- a/src/syscall_imp/mod.rs +++ b/src/syscall_imp/mod.rs @@ -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")] diff --git a/src/syscall_imp/task/schedule.rs b/src/syscall_imp/task/schedule.rs index 5de19b38..d4bb3719 100644 --- a/src/syscall_imp/task/schedule.rs +++ b/src/syscall_imp/task/schedule.rs @@ -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) } } diff --git a/src/task.rs b/src/task.rs index 34434f41..c7ecaf29 100644 --- a/src/task.rs +++ b/src/task.rs @@ -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}; @@ -20,15 +22,18 @@ pub struct TaskExt { pub uctx: UspaceContext, /// The virtual memory address space. pub aspace: Arc>, + /// The resource namespace + pub ns: AxNamespace, } impl TaskExt { - pub const fn new(uctx: UspaceContext, aspace: Arc>) -> Self { + pub fn new(uctx: UspaceContext, aspace: Arc>) -> Self { Self { proc_id: 233, uctx, clear_child_tid: AtomicU64::new(0), aspace, + ns: AxNamespace::new_thread_local(), } } @@ -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>, uctx: UspaceContext) -> AxTaskRef {