Skip to content

Commit

Permalink
[namespace] Support axns for fd table and cwd resources. Initialize t…
Browse files Browse the repository at this point in the history
…hem before entering app main
  • Loading branch information
Azure-stars committed Nov 17, 2024
1 parent 801de3c commit ea19a87
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 18 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ axdma = { path = "modules/axdma" }

[profile.release]
lto = true

[profile.dev]
codegen-units = 1
6 changes: 5 additions & 1 deletion api/arceos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ documentation = "https://arceos-org.github.io/arceos/arceos_posix_api/index.html
[features]
default = []

uspace = ["axns/thread-local", "axfs/thread-local", "smp", "irq", "fs", "multitask", "net", "pipe", "select", "epoll"]
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"]
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,8 @@ flatten_objects = "0.1"
static_assertions = "1.1.0"
spin = { version = "0.9" }
lazy_static = { version = "1.5", features = ["spin_no_std"] }
crate_interface = "0.1"
linkme = "0.3"

[build-dependencies]
bindgen ={ version = "0.69" }
13 changes: 4 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,10 +3,10 @@ use core::ffi::c_int;

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

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

pub const AX_FILE_LIMIT: usize = 1024;
Expand All @@ -21,14 +21,9 @@ 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(); // stdin
fd_table.add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout
fd_table.add_at(2, Arc::new(stdout()) as _).unwrap(); // stderr
RwLock::new(fd_table)
};
def_resource! {
#[allow(non_camel_case_types)]
pub(crate) static FD_TABLE: AxResource<RwLock<FlattenObjects<Arc<dyn FileLike>, AX_FILE_LIMIT>>> = AxResource::new();
}

pub fn get_file_like(fd: c_int) -> LinuxResult<Arc<dyn FileLike>> {
Expand Down
13 changes: 13 additions & 0 deletions api/arceos_posix_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ pub mod net;
pub mod pipe;
#[cfg(feature = "multitask")]
pub mod pthread;

#[cfg(feature = "fd")]
#[linkme::distributed_slice(axns::INIT_RESOURCE)]
fn init_stdio() {
use crate::imp::fd_ops::FD_TABLE;
use alloc::sync::Arc;
use stdio::{stdin, stdout};
let mut fd_table = flatten_objects::FlattenObjects::new();
fd_table.add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin
fd_table.add_at(1, Arc::new(stdout()) as _).unwrap(); // stdout
fd_table.add_at(2, Arc::new(stdout()) as _).unwrap(); // stderr
FD_TABLE.init_new(spin::RwLock::new(fd_table));
}
4 changes: 4 additions & 0 deletions modules/axfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/arceos-org/arceos/tree/main/modules/axfs"
documentation = "https://arceos-org.github.io/arceos/axfs/index.html"

[features]
thread-local = ["axns/thread-local"]
devfs = ["dep:axfs_devfs"]
ramfs = ["dep:axfs_ramfs"]
procfs = ["dep:axfs_ramfs"]
Expand All @@ -34,6 +35,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 All @@ -52,3 +54,5 @@ axdriver = { workspace = true, features = ["block", "ramdisk"] }
axdriver_block = { git = "https://github.com/arceos-org/axdriver_crates.git", tag = "v0.1.0", features = ["ramdisk"] }
axsync = { workspace = true, features = ["multitask"] }
axtask = { workspace = true, features = ["test"] }
crate_interface = "0.1"
axns = { workspace = true, features = ["thread-local"] }
19 changes: 19 additions & 0 deletions modules/axfs/src/fops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ pub type FileType = axfs_vfs::VfsNodeType;
pub type DirEntry = axfs_vfs::VfsDirEntry;
/// Alias of [`axfs_vfs::VfsNodeAttr`].
pub type FileAttr = axfs_vfs::VfsNodeAttr;
/// Alias of [`axfs_vfs::VfsNodeAttr`].
pub type DirAttr = axfs_vfs::VfsNodeAttr;
/// Alias of [`axfs_vfs::VfsNodePerm`].
pub type FilePerm = axfs_vfs::VfsNodePerm;
/// Alias of [`axfs_vfs::VfsNodePerm`].
pub type DirPerm = axfs_vfs::VfsNodePerm;

/// An opened file object, with open permissions and a cursor.
pub struct File {
Expand Down Expand Up @@ -246,6 +250,11 @@ impl File {
pub fn get_attr(&self) -> AxResult<FileAttr> {
self.access_node(Cap::empty())?.get_attr()
}

/// Gets the inode number of the file.
pub fn get_inode(&self) -> u64 {
0
}
}

impl Directory {
Expand Down Expand Up @@ -345,6 +354,16 @@ impl Directory {
pub fn rename(&self, old: &str, new: &str) -> AxResult {
crate::root::rename(old, new)
}

/// Gets the directory attributes.
pub fn get_attr(&self) -> AxResult<DirAttr> {
self.access_node(Cap::empty())?.get_attr()
}

/// Gets the inode number of the file.
pub fn get_inode(&self) -> u64 {
0
}
}

impl Drop for File {
Expand Down
13 changes: 9 additions & 4 deletions modules/axfs/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
use alloc::{string::String, sync::Arc, vec::Vec};
use axerrno::{ax_err, AxError, AxResult};
use axfs_vfs::{VfsNodeAttr, VfsNodeOps, VfsNodeRef, VfsNodeType, VfsOps, VfsResult};
use axns::{def_resource, AxResource};
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! {
#[allow(non_camel_case_types)]
static CURRENT_DIR_PATH: AxResource<Mutex<String>> = AxResource::new();
#[allow(non_camel_case_types)]
static CURRENT_DIR: AxResource<Mutex<VfsNodeRef>> = AxResource::new();
}

struct MountPoint {
path: &'static str,
Expand Down Expand Up @@ -180,8 +185,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
26 changes: 25 additions & 1 deletion modules/axfs/tests/test_fatfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,34 @@ fn make_disk() -> std::io::Result<RamDisk> {
Ok(RamDisk::from(&data))
}

mod axns_imp {
use axns::{AxNamespace, AxNamespaceIf};
use lazyinit::LazyInit;

thread_local! {
static NS: LazyInit<AxNamespace> = LazyInit::new();
}

struct AxNamespaceImpl;

#[crate_interface::impl_interface]
impl AxNamespaceIf for AxNamespaceImpl {
fn current_namespace_base() -> *mut u8 {
NS.with(|ns| ns.base())
}
}

pub(crate) fn thread_init_namespace() {
NS.with(|ns| {
ns.init_once(AxNamespace::global());
});
}
}

#[test]
fn test_fatfs() {
println!("Testing fatfs with ramdisk ...");

axns_imp::thread_init_namespace();
let disk = make_disk().expect("failed to load disk image");
axtask::init_scheduler(); // call this to use `axsync::Mutex`.
axfs::init_filesystems(AxDeviceContainer::from_one(disk));
Expand Down
25 changes: 24 additions & 1 deletion modules/axfs/tests/test_ramfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,33 @@ fn create_init_files() -> Result<()> {
Ok(())
}

mod axns_imp {
use axns::{AxNamespace, AxNamespaceIf};
use lazyinit::LazyInit;

thread_local! {
static NS: LazyInit<AxNamespace> = LazyInit::new();
}
struct AxNamespaceImpl;

#[crate_interface::impl_interface]
impl AxNamespaceIf for AxNamespaceImpl {
fn current_namespace_base() -> *mut u8 {
NS.with(|ns| ns.base())
}
}

pub(crate) fn thread_init_namespace() {
NS.with(|ns| {
ns.init_once(AxNamespace::global());
});
}
}

#[test]
fn test_ramfs() {
println!("Testing ramfs ...");

axns_imp::thread_init_namespace();
axtask::init_scheduler(); // call this to use `axsync::Mutex`.
axfs::init_filesystems(AxDeviceContainer::from_one(RamDisk::default())); // dummy disk, actually not used.

Expand Down
1 change: 1 addition & 0 deletions modules/axhal/src/arch/x86_64/syscall.S
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.section .text
.code64
syscall_entry:
swapgs // switch to kernel gs
mov gs:[offset __PERCPU_USER_RSP_OFFSET], rsp // save user rsp
Expand Down
9 changes: 9 additions & 0 deletions modules/axmm/src/aspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ impl AddrSpace {
/// To process data in this area with the given function.
///
/// Now it supports reading and writing data in the given interval.
///
/// # Arguments
/// - `start`: The start virtual address to process.
/// - `size`: The size of the data to process.
/// - `f`: The function to process the data, whose arguments are the start virtual address,
/// the offset and the size of the data.
///
/// # Notes
/// The caller must ensure that the permission of the operation is allowed.
fn process_area_data<F>(&self, start: VirtAddr, size: usize, mut f: F) -> AxResult
where
F: FnMut(VirtAddr, usize, usize),
Expand Down
3 changes: 2 additions & 1 deletion modules/axns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ default = []
thread-local = []

[dependencies]
log = "0.4"
log = "0.4.21"
lazyinit = "0.2"
crate_interface = "0.1"
linkme = "0.3"

[dev-dependencies]
axns = { workspace = true, features = ["thread-local"] }
5 changes: 5 additions & 0 deletions modules/axns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern crate alloc;

use alloc::sync::Arc;
use core::{alloc::Layout, fmt, ops::Deref};
use linkme::distributed_slice;

use lazyinit::LazyInit;

Expand All @@ -31,6 +32,10 @@ extern "C" {
fn __stop_axns_resource();
}

/// A distributed slice that contains all method to initialize user-defined resources.
#[distributed_slice]
pub static INIT_RESOURCE: [fn()];

/// A namespace that contains all user-defined resources.
///
/// There are two types of namespaces:
Expand Down
3 changes: 2 additions & 1 deletion modules/axruntime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ default = []
smp = ["axhal/smp"]
irq = ["axhal/irq", "axtask?/irq", "percpu", "kernel_guard"]
tls = ["axhal/tls", "axtask?/tls"]
alloc = ["axalloc"]
alloc = ["axalloc", "axns"]
paging = ["axhal/paging", "axmm"]

multitask = ["axtask/multitask"]
Expand All @@ -35,6 +35,7 @@ axfs = { workspace = true, optional = true }
axnet = { workspace = true, optional = true }
axdisplay = { workspace = true, optional = true }
axtask = { workspace = true, optional = true }
axns = { workspace = true, optional = true }

crate_interface = "0.1"
percpu = { version = "0.1", optional = true }
Expand Down
5 changes: 5 additions & 0 deletions modules/axruntime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! {
info!("Primary CPU {} init OK.", cpu_id);
INITED_CPUS.fetch_add(1, Ordering::Relaxed);

#[cfg(feature = "alloc")]
for init in axns::INIT_RESOURCE.iter() {
init();
}

while !is_init_ok() {
core::hint::spin_loop();
}
Expand Down

0 comments on commit ea19a87

Please sign in to comment.