Skip to content

Commit

Permalink
[feat] Add support for resource namespace and monolithic plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-stars committed Feb 14, 2025
1 parent 40e512d commit 63732c8
Show file tree
Hide file tree
Showing 36 changed files with 731 additions and 191 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"modules/axmm",
"modules/axdma",
"modules/axnet",
"modules/axns",
"modules/axruntime",
"modules/axsync",
"modules/axtask",
Expand Down Expand Up @@ -58,6 +59,7 @@ axhal = { path = "modules/axhal" }
axlog = { path = "modules/axlog" }
axmm = { path = "modules/axmm" }
axnet = { path = "modules/axnet" }
axns = { path = "modules/axns" }
axruntime = { path = "modules/axruntime" }
axsync = { path = "modules/axsync" }
axtask = { path = "modules/axtask" }
Expand Down
5 changes: 4 additions & 1 deletion api/arceos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ smp = ["axfeat/smp"]
irq = ["axfeat/irq"]
alloc = ["dep:axalloc", "axfeat/alloc"]
multitask = ["axtask/multitask", "axfeat/multitask", "axsync/multitask"]
fd = ["alloc"]
fd = ["alloc", "dep:axns"]
fs = ["dep:axfs", "axfeat/fs", "fd"]
net = ["dep:axnet", "axfeat/net", "fd"]
pipe = ["fd"]
select = ["fd"]
epoll = ["fd"]
uspace = ["axns/thread-local"]

[dependencies]
# ArceOS modules
Expand All @@ -42,6 +43,7 @@ axalloc = { workspace = true, optional = true }
axtask = { workspace = true, optional = true }
axfs = { workspace = true, optional = true }
axnet = { workspace = true, optional = true }
axns = { workspace = true, optional = true }

# Other crates
axio = "0.1"
Expand All @@ -50,6 +52,7 @@ flatten_objects = "0.2"
static_assertions = "1.1.0"
spin = { version = "0.9" }
lazy_static = { version = "1.5", features = ["spin_no_std"] }
ctor_bare = "0.2"

[build-dependencies]
bindgen ={ version = "0.69" }
28 changes: 19 additions & 9 deletions api/arceos_posix_api/src/imp/fd_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use core::ffi::c_int;

use axerrno::{LinuxError, LinuxResult};
use axio::PollState;
use axns::{ResArc, def_resource};
use flatten_objects::FlattenObjects;
use spin::RwLock;

use super::stdio::{stdin, stdout};
use crate::ctypes;
use crate::imp::stdio::{stdin, stdout};

pub const AX_FILE_LIMIT: usize = 1024;

Expand All @@ -21,14 +22,8 @@ pub trait FileLike: Send + Sync {
fn set_nonblocking(&self, nonblocking: bool) -> LinuxResult;
}

lazy_static::lazy_static! {
static ref FD_TABLE: RwLock<FlattenObjects<Arc<dyn FileLike>, AX_FILE_LIMIT>> = {
let mut fd_table = FlattenObjects::new();
fd_table.add_at(0, Arc::new(stdin()) as _).unwrap_or_else(|_| panic!()); // stdin
fd_table.add_at(1, Arc::new(stdout()) as _).unwrap_or_else(|_| panic!()); // stdout
fd_table.add_at(2, Arc::new(stdout()) as _).unwrap_or_else(|_| panic!()); // stderr
RwLock::new(fd_table)
};
def_resource! {
pub(crate) static FD_TABLE: ResArc<RwLock<FlattenObjects<Arc<dyn FileLike>, AX_FILE_LIMIT>>> = ResArc::new();
}

pub fn get_file_like(fd: c_int) -> LinuxResult<Arc<dyn FileLike>> {
Expand Down Expand Up @@ -127,3 +122,18 @@ pub fn sys_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int {
}
})
}

#[ctor_bare::register_ctor]
fn init_stdio() {
let mut fd_table = flatten_objects::FlattenObjects::new();
fd_table
.add_at(0, Arc::new(stdin()) as _)
.unwrap_or_else(|_| panic!()); // stdin
fd_table
.add_at(1, Arc::new(stdout()) as _)
.unwrap_or_else(|_| panic!()); // stdout
fd_table
.add_at(2, Arc::new(stdout()) as _)
.unwrap_or_else(|_| panic!()); // stderr
FD_TABLE.init_new(spin::RwLock::new(fd_table));
}
4 changes: 2 additions & 2 deletions api/arceos_posix_api/src/imp/io_mpx/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ pub unsafe fn sys_select(
unsafe fn zero_fd_set(fds: *mut ctypes::fd_set, nfds: usize) {
if !fds.is_null() {
let nfds_usizes = nfds.div_ceil(BITS_PER_USIZE);
let dst = &mut (*fds).fds_bits[..nfds_usizes];
let dst = &mut unsafe { *fds }.fds_bits[..nfds_usizes];
dst.fill(0);
}
}

unsafe fn set_fd_set(fds: *mut ctypes::fd_set, fd: usize) {
if !fds.is_null() {
(*fds).fds_bits[fd / BITS_PER_USIZE] |= 1 << (fd % BITS_PER_USIZE);
unsafe { *fds }.fds_bits[fd / BITS_PER_USIZE] |= 1 << (fd % BITS_PER_USIZE);
}
}
6 changes: 3 additions & 3 deletions api/arceos_posix_api/src/imp/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ pub unsafe fn sys_freeaddrinfo(res: *mut ctypes::addrinfo) {
return;
}
let aibuf_ptr = res as *mut ctypes::aibuf;
let len = (*aibuf_ptr).ref_ as usize;
assert!((*aibuf_ptr).slot == 0);
let len = unsafe { *aibuf_ptr }.ref_ as usize;
assert!(unsafe { *aibuf_ptr }.slot == 0);
assert!(len > 0);
let vec = Vec::from_raw_parts(aibuf_ptr, len, len); // TODO: lock
let vec = unsafe { Vec::from_raw_parts(aibuf_ptr, len, len) }; // TODO: lock
drop(vec);
}

Expand Down
1 change: 1 addition & 0 deletions modules/axfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ crate_interface = { version = "0.1", optional = true }
axsync = { workspace = true }
axdriver = { workspace = true, features = ["block"] }
axdriver_block = { git = "https://github.com/arceos-org/axdriver_crates.git", tag = "v0.1.0" }
axns = { workspace = true }

[dependencies.fatfs]
git = "https://github.com/rafalh/rust-fatfs"
Expand Down
11 changes: 7 additions & 4 deletions modules/axfs/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use alloc::{string::String, sync::Arc, vec::Vec};
use axerrno::{AxError, AxResult, ax_err};
use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType, VfsOps, VfsResult};
use axns::{ResArc, def_resource};
use axsync::Mutex;
use lazyinit::LazyInit;

use crate::{api::FileType, fs, mounts};

static CURRENT_DIR_PATH: Mutex<String> = Mutex::new(String::new());
static CURRENT_DIR: LazyInit<Mutex<VfsNodeRef>> = LazyInit::new();
def_resource! {
static CURRENT_DIR_PATH: ResArc<Mutex<String>> = ResArc::new();
static CURRENT_DIR: ResArc<Mutex<VfsNodeRef>> = ResArc::new();
}

struct MountPoint {
path: &'static str,
Expand Down Expand Up @@ -180,8 +183,8 @@ pub(crate) fn init_rootfs(disk: crate::dev::Disk) {
.expect("fail to mount sysfs at /sys");

ROOT_DIR.init_once(Arc::new(root_dir));
CURRENT_DIR.init_once(Mutex::new(ROOT_DIR.clone()));
*CURRENT_DIR_PATH.lock() = "/".into();
CURRENT_DIR.init_new(Mutex::new(ROOT_DIR.clone()));
CURRENT_DIR_PATH.init_new(Mutex::new("/".into()));
}

fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef {
Expand Down
14 changes: 11 additions & 3 deletions modules/axhal/linker.lds.S
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ SECTIONS
_etext = .;
}

_srodata = .;
.rodata : ALIGN(4K) {
_srodata = .;
*(.rodata .rodata.*)
*(.srodata .srodata.*)
*(.sdata2 .sdata2.*)
. = ALIGN(4K);
_erodata = .;
}

.init_array : ALIGN(0x10) {
__init_array_start = .;
*(.init_array .init_array.*)
__init_array_end = .;
}

. = ALIGN(4K);
_erodata = .;

.data : ALIGN(4K) {
_sdata = .;
*(.data.boot_page_table)
Expand Down Expand Up @@ -89,5 +96,6 @@ SECTIONS {
linkm2_PAGE_FAULT : { *(linkm2_PAGE_FAULT) }
linkme_SYSCALL : { *(linkme_SYSCALL) }
linkm2_SYSCALL : { *(linkm2_SYSCALL) }
axns_resource : { *(axns_resource) }
}
INSERT AFTER .tbss;
60 changes: 31 additions & 29 deletions modules/axhal/src/arch/aarch64/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,41 +118,43 @@ impl UspaceContext {
///
/// This function is unsafe because it changes processor mode and the stack.
#[inline(never)]
#[no_mangle]
#[unsafe(no_mangle)]
pub unsafe fn enter_uspace(&self, kstack_top: VirtAddr) -> ! {
super::disable_irqs();
// We do not handle traps that occur at the current exception level,
// so the kstack ptr(`sp_el1`) will not change during running in user space.
// Then we don't need to save the `sp_el1` to the taskctx.
asm!(
"
mov sp, x1
ldp x30, x9, [x0, 30 * 8]
ldp x10, x11, [x0, 32 * 8]
msr sp_el0, x9
msr elr_el1, x10
msr spsr_el1, x11
unsafe {
core::arch::asm!(
"
mov sp, x1
ldp x30, x9, [x0, 30 * 8]
ldp x10, x11, [x0, 32 * 8]
msr sp_el0, x9
msr elr_el1, x10
msr spsr_el1, x11
ldp x28, x29, [x0, 28 * 8]
ldp x26, x27, [x0, 26 * 8]
ldp x24, x25, [x0, 24 * 8]
ldp x22, x23, [x0, 22 * 8]
ldp x20, x21, [x0, 20 * 8]
ldp x18, x19, [x0, 18 * 8]
ldp x16, x17, [x0, 16 * 8]
ldp x14, x15, [x0, 14 * 8]
ldp x12, x13, [x0, 12 * 8]
ldp x10, x11, [x0, 10 * 8]
ldp x8, x9, [x0, 8 * 8]
ldp x6, x7, [x0, 6 * 8]
ldp x4, x5, [x0, 4 * 8]
ldp x2, x3, [x0, 2 * 8]
ldp x0, x1, [x0]
eret",
in("x0") &self.0,
in("x1") kstack_top.as_usize() ,
options(noreturn),
)
ldp x28, x29, [x0, 28 * 8]
ldp x26, x27, [x0, 26 * 8]
ldp x24, x25, [x0, 24 * 8]
ldp x22, x23, [x0, 22 * 8]
ldp x20, x21, [x0, 20 * 8]
ldp x18, x19, [x0, 18 * 8]
ldp x16, x17, [x0, 16 * 8]
ldp x14, x15, [x0, 14 * 8]
ldp x12, x13, [x0, 12 * 8]
ldp x10, x11, [x0, 10 * 8]
ldp x8, x9, [x0, 8 * 8]
ldp x6, x7, [x0, 6 * 8]
ldp x4, x5, [x0, 4 * 8]
ldp x2, x3, [x0, 2 * 8]
ldp x0, x1, [x0]
eret",
in("x0") &self.0,
in("x1") kstack_top.as_usize() ,
options(noreturn),
)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/axhal/src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub unsafe fn write_thread_pointer(tpidr_el0: usize) {
///
/// On AArch64, it sets the exception vector base address (`VBAR_EL1`) and `TTBR0_EL1`.
pub fn cpu_init() {
extern "C" {
unsafe extern "C" {
fn exception_vector_base();
}
set_exception_vector_base(exception_vector_base as usize);
Expand Down
Loading

0 comments on commit 63732c8

Please sign in to comment.