Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[task]: Introduce namespaces io manage system resources #193

Open
wants to merge 17 commits into
base: monolithic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 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
1 change: 1 addition & 0 deletions modules/axhal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ paging = ["axalloc", "page_table_multiarch"]
irq = []
tls = ["alloc"]
rtc = ["x86_rtc", "riscv_goldfish", "arm_pl031"]
uspace = ["paging"]
default = []

[dependencies]
Expand Down
16 changes: 13 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 @@ -87,5 +94,8 @@ SECTIONS {
linkm2_IRQ : { *(linkm2_IRQ) }
linkme_PAGE_FAULT : { *(linkme_PAGE_FAULT) }
linkm2_PAGE_FAULT : { *(linkm2_PAGE_FAULT) }
linkme_SYSCALL : { *(linkme_SYSCALL) }
linkm2_SYSCALL : { *(linkm2_SYSCALL) }
axns_resource : { *(axns_resource) }
}
INSERT AFTER .tbss;
Loading
Loading