Skip to content
10 changes: 5 additions & 5 deletions etc/syscalls_linux_aarch64.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
| 0x10 (16) | fremovexattr | (int fd, const char *name) | __arm64_sys_fremovexattr | true |
| 0x11 (17) | getcwd | (char *buf, unsigned long size) | __arm64_sys_getcwd | true |
| 0x13 (19) | eventfd2 | (unsigned int count, int flags) | __arm64_sys_eventfd2 | false |
| 0x14 (20) | epoll_create1 | (int flags) | __arm64_sys_epoll_create1 | false |
| 0x15 (21) | epoll_ctl | (int epfd, int op, int fd, struct epoll_event *event) | __arm64_sys_epoll_ctl | false |
| 0x16 (22) | epoll_pwait | (int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *sigmask, size_t sigsetsize) | __arm64_sys_epoll_pwait | false |
| 0x14 (20) | epoll_create1 | (int flags) | __arm64_sys_epoll_create1 | true |
| 0x15 (21) | epoll_ctl | (int epfd, int op, int fd, struct epoll_event *event) | __arm64_sys_epoll_ctl | true |
| 0x16 (22) | epoll_pwait | (int epfd, struct epoll_event *events, int maxevents, int timeout, const sigset_t *sigmask, size_t sigsetsize) | __arm64_sys_epoll_pwait | partially |
| 0x17 (23) | dup | (unsigned int fildes) | __arm64_sys_dup | true |
| 0x18 (24) | dup3 | (unsigned int oldfd, unsigned int newfd, int flags) | __arm64_sys_dup3 | true |
| 0x19 (25) | fcntl | (unsigned int fd, unsigned int cmd, unsigned long arg) | __arm64_sys_fcntl | true |
Expand All @@ -41,7 +41,7 @@
| 0x25 (37) | linkat | (int olddfd, const char *oldname, int newdfd, const char *newname, int flags) | __arm64_sys_linkat | true |
| 0x26 (38) | renameat | (int olddfd, const char *oldname, int newdfd, const char *newname) | __arm64_sys_renameat | true |
| 0x27 (39) | umount | (char *name, int flags) | __arm64_sys_umount | false |
| 0x28 (40) | mount | (char *dev_name, char *dir_name, char *type, unsigned long flags, void *data) | __arm64_sys_mount | false |
| 0x28 (40) | mount | (char *dev_name, char *dir_name, char *type, unsigned long flags, void *data) | __arm64_sys_mount | partial |
| 0x29 (41) | pivot_root | (const char *new_root, const char *put_old) | __arm64_sys_pivot_root | false |
| 0x2b (43) | statfs | (const char *pathname, struct statfs *buf) | __arm64_sys_statfs | partial |
| 0x2c (44) | fstatfs | (unsigned int fd, struct statfs *buf) | __arm64_sys_fstatfs | partial |
Expand Down Expand Up @@ -289,7 +289,7 @@
| 0x1af (431) | fsconfig | (int fd, unsigned int cmd, const char *_key, const void *_value, int aux) | __arm64_sys_fsconfig | false |
| 0x1b0 (432) | fsmount | (int fs_fd, unsigned int flags, unsigned int attr_flags) | __arm64_sys_fsmount | false |
| 0x1b1 (433) | fspick | (int dfd, const char *path, unsigned int flags) | __arm64_sys_fspick | false |
| 0x1b2 (434) | pidfd_open | (pid_t pid, unsigned int flags) | __arm64_sys_pidfd_open | false |
| 0x1b2 (434) | pidfd_open | (pid_t pid, unsigned int flags) | __arm64_sys_pidfd_open | true |
| 0x1b3 (435) | clone3 | (struct clone_args *uargs, size_t size) | __arm64_sys_clone3 | false |
| 0x1b4 (436) | close_range | (unsigned int fd, unsigned int max_fd, unsigned int flags) | __arm64_sys_close_range | partially |
| 0x1b5 (437) | openat2 | (int dfd, const char *filename, struct open_how *how, size_t usize) | __arm64_sys_openat2 | false |
Expand Down
6 changes: 6 additions & 0 deletions libkernel/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ impl<T: ?Sized, CPU: CpuOps> DerefMut for AsyncMutexGuard<'_, T, CPU> {
unsafe impl<T: ?Sized + Send, CPU: CpuOps> Send for Mutex<T, CPU> {}
unsafe impl<T: ?Sized + Send, CPU: CpuOps> Sync for Mutex<T, CPU> {}

impl<T: ?Sized + Default, CPU: CpuOps> Default for Mutex<T, CPU> {
fn default() -> Self {
Self::new(T::default())
}
}

impl<CPU: CpuOps> Mutex<(), CPU> {
/// Acquires the mutex lock without caring about the data.
pub(crate) fn acquire(&self) -> MutexAcquireFuture<'_, CPU> {
Expand Down
2 changes: 1 addition & 1 deletion scripts/qemu-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ bin="${elf%.elf}.bin"

# Convert to binary format
aarch64-none-elf-objcopy -O binary "$elf" "$bin"
qemu-system-aarch64 -M virt,gic-version=3 -initrd moss.img -cpu cortex-a72 -m 2G -smp 4 -nographic -s -kernel "$bin" -append "$append_args --rootfs=ext4fs --automount=/dev,devfs --automount=/tmp,tmpfs --automount=/proc,procfs --automount=/sys,sysfs"
qemu-system-aarch64 -M virt,gic-version=3 -initrd arch.img -cpu cortex-a72 -m 2G -smp 4 -nographic -s -kernel "$bin" -append "$append_args --rootfs=ext4fs --automount=/dev,devfs --automount=/tmp,tmpfs --automount=/proc,procfs --automount=/sys,sysfs"
42 changes: 41 additions & 1 deletion src/arch/arm64/exceptions/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::{
ioctl::sys_ioctl,
iov::{sys_preadv, sys_preadv2, sys_pwritev, sys_pwritev2, sys_readv, sys_writev},
listxattr::{sys_flistxattr, sys_listxattr, sys_llistxattr},
mount::sys_mount,
removexattr::{sys_fremovexattr, sys_lremovexattr, sys_removexattr},
rw::{sys_pread64, sys_pwrite64, sys_read, sys_write},
seek::sys_lseek,
Expand All @@ -56,7 +57,7 @@ use crate::{
process_vm::sys_process_vm_readv,
},
process::{
TaskState,
TaskState, Tid,
caps::{sys_capget, sys_capset},
clone::sys_clone,
creds::{
Expand All @@ -67,11 +68,14 @@ use crate::{
exit::{sys_exit, sys_exit_group},
fd_table::{
dup::{sys_dup, sys_dup3},
epoll::{sys_epoll_create1, sys_epoll_ctl, sys_epoll_pwait},
fcntl::sys_fcntl,
select::{sys_ppoll, sys_pselect6},
},
pidfd::sys_pidfd_open,
prctl::sys_prctl,
ptrace::{TracePoint, ptrace_stop, sys_ptrace},
signalfd::sys_signalfd4,
sleep::{sys_clock_nanosleep, sys_nanosleep},
thread_group::{
Pgid,
Expand Down Expand Up @@ -197,10 +201,33 @@ pub async fn handle_syscall() {
0xf => sys_lremovexattr(TUA::from_value(arg1 as _), TUA::from_value(arg2 as _)).await,
0x10 => sys_fremovexattr(arg1.into(), TUA::from_value(arg2 as _)).await,
0x11 => sys_getcwd(TUA::from_value(arg1 as _), arg2 as _).await,
0x14 => sys_epoll_create1(arg1 as _),
0x15 => {
sys_epoll_ctl(
arg1.into(),
arg2 as _,
arg3.into(),
TUA::from_value(arg4 as _),
)
.await
}
0x16 => {
sys_epoll_pwait(
arg1.into(),
TUA::from_value(arg2 as _),
arg3 as _,
arg4 as _,
TUA::from_value(arg5 as _),
arg6 as _,
)
.await
}
0x17 => sys_dup(arg1.into()),
0x18 => sys_dup3(arg1.into(), arg2.into(), arg3 as _),
0x19 => sys_fcntl(arg1.into(), arg2 as _, arg3 as _).await,
0x1d => sys_ioctl(arg1.into(), arg2 as _, arg3 as _).await,
0x20 => Ok(0),
0x21 => Err(KernelError::NotSupported),
0x22 => sys_mkdirat(arg1.into(), TUA::from_value(arg2 as _), arg3 as _).await,
0x23 => sys_unlinkat(arg1.into(), TUA::from_value(arg2 as _), arg3 as _).await,
0x24 => {
Expand Down Expand Up @@ -230,6 +257,16 @@ pub async fn handle_syscall() {
)
.await
}
0x28 => {
sys_mount(
TUA::from_value(arg1 as _),
TUA::from_value(arg2 as _),
TUA::from_value(arg3 as _),
arg4 as _,
TUA::from_value(arg5 as _),
)
.await
}
0x2b => sys_statfs(TUA::from_value(arg1 as _), TUA::from_value(arg2 as _)).await,
0x2c => sys_fstatfs(arg1.into(), TUA::from_value(arg2 as _)).await,
0x2d => sys_truncate(TUA::from_value(arg1 as _), arg2 as _).await,
Expand Down Expand Up @@ -342,6 +379,7 @@ pub async fn handle_syscall() {
)
.await
}
0x4a => sys_signalfd4(arg1.into(), TUA::from_value(arg2 as _), arg3 as _).await,
0x4e => {
sys_readlinkat(
arg1.into(),
Expand Down Expand Up @@ -623,6 +661,8 @@ pub async fn handle_syscall() {
.await
}
0x125 => Err(KernelError::NotSupported),
0x1ae => Err(KernelError::NotSupported),
0x1b2 => sys_pidfd_open(Tid(arg1 as _), arg2 as _).await,
0x1b4 => sys_close_range(arg1.into(), arg2.into(), arg3 as _).await,
0x1b7 => {
sys_faccessat2(
Expand Down
9 changes: 9 additions & 0 deletions src/console/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
use alloc::{boxed::Box, sync::Arc};
use async_trait::async_trait;
use cooker::TtyInputCooker;
use core::any::Any;
use core::{cmp::min, pin::Pin};
use futures::{
future::{Either, select},
Expand Down Expand Up @@ -86,6 +87,14 @@ impl Tty {

#[async_trait]
impl FileOps for Tty {
fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

async fn read(&mut self, _ctx: &mut FileCtx, usr_buf: UA, count: usize) -> Result<usize> {
self.readat(usr_buf, count, 0).await
}
Expand Down
15 changes: 13 additions & 2 deletions src/drivers/fs/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,30 @@ pub struct DevFs {

impl DevFs {
pub fn new() -> Arc<Self> {
let shm = DevFsINode {
id: InodeId::from_fsid_and_inodeid(DEVFS_ID, 1),
attr: SpinLock::new(FileAttr {
file_type: FileType::Directory,
mode: FilePermissions::from_bits_retain(0o755),
..FileAttr::default()
}),
kind: InodeKind::Directory(SpinLock::new(BTreeMap::new())),
};
let mut root_children = BTreeMap::new();
root_children.insert("shm".to_string(), Arc::new(shm));
let root_inode = Arc::new(DevFsINode {
id: InodeId::from_fsid_and_inodeid(DEVFS_ID, 0),
attr: SpinLock::new(FileAttr {
file_type: FileType::Directory,
mode: FilePermissions::from_bits_retain(0o755),
..FileAttr::default()
}),
kind: InodeKind::Directory(SpinLock::new(BTreeMap::new())),
kind: InodeKind::Directory(SpinLock::new(root_children)),
});

Arc::new(Self {
root: root_inode,
next_inode_id: AtomicU64::new(1),
next_inode_id: AtomicU64::new(2),
})
}

Expand Down
9 changes: 9 additions & 0 deletions src/drivers/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
use alloc::string::ToString;
use alloc::{boxed::Box, sync::Arc};
use async_trait::async_trait;
use core::any::Any;
use core::{future::Future, pin::Pin};
use libkernel::{
driver::CharDevDescriptor,
Expand All @@ -22,6 +23,14 @@ struct NullFileOps;

#[async_trait]
impl FileOps for NullFileOps {
fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

async fn readat(&mut self, _buf: UA, _count: usize, _offset: u64) -> Result<usize> {
// EOF
Ok(0)
Expand Down
9 changes: 9 additions & 0 deletions src/drivers/zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
};
use alloc::{boxed::Box, string::ToString, sync::Arc};
use async_trait::async_trait;
use core::any::Any;
use core::{cmp::min, future::Future, pin::Pin};
use libkernel::{
driver::CharDevDescriptor,
Expand All @@ -25,6 +26,14 @@ struct ZeroFileOps;

#[async_trait]
impl FileOps for ZeroFileOps {
fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

async fn read(&mut self, _ctx: &mut FileCtx, buf: UA, count: usize) -> Result<usize> {
self.readat(buf, count, 0).await
}
Expand Down
9 changes: 9 additions & 0 deletions src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloc::boxed::Box;
use alloc::ffi::CString;
use async_trait::async_trait;
use core::alloc::Layout;
use core::any::Any;
use libkernel::{
error::{FsError, KernelError, Result},
fs::{DirStream, Dirent, FileType, Inode},
Expand Down Expand Up @@ -60,6 +61,14 @@ impl DirFile {

#[async_trait]
impl FileOps for DirFile {
fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

async fn read(&mut self, _ctx: &mut FileCtx, _buf: UA, _count: usize) -> Result<usize> {
Err(FsError::IsADirectory.into())
}
Expand Down
10 changes: 9 additions & 1 deletion src/fs/fops.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::any::Any;
use core::pin::Pin;

use alloc::boxed::Box;
Expand Down Expand Up @@ -39,7 +40,10 @@ macro_rules! process_iovec {
}

#[async_trait]
pub trait FileOps: Send + Sync {
pub trait FileOps: Send + Sync + Any {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;

/// Reads data from the current file position into `buf`.
/// The file's cursor is advanced by the number of bytes read.
async fn read(&mut self, ctx: &mut FileCtx, buf: UA, count: usize) -> Result<usize> {
Expand Down Expand Up @@ -137,4 +141,8 @@ pub trait FileOps: Send + Sync {
) -> Result<usize> {
Err(KernelError::InvalidValue)
}

fn to_epoll(&self) -> Option<&crate::process::fd_table::epoll::EpollFileOps> {
None
}
}
20 changes: 20 additions & 0 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ impl VfsState {
self.mounts.insert(mount_point_id, mount);
}

/// Removes a mount point by its inode ID.
fn remove_mount(&mut self, mount_point_id: &InodeId) -> Option<()> {
let mount = self.mounts.remove(mount_point_id)?;
self.filesystems.remove(&mount.fs.id())?;
Some(())
}

/// Checks if an inode is a mount point and returns the root inode of the
/// mounted filesystem if it is.
fn get_mount_root(&self, inode_id: &InodeId) -> Option<Arc<dyn Inode>> {
Expand Down Expand Up @@ -177,6 +184,19 @@ impl VFS {
Ok(())
}

#[expect(unused)]
pub async fn unmount(&self, mount_point: Arc<dyn Inode>) -> Result<()> {
let mount_point_id = mount_point.id();

// Lock the state and remove the mount.
self.state
.lock_save_irq()
.remove_mount(&mount_point_id)
.ok_or(FsError::NotFound)?;

Ok(())
}

pub async fn get_fs(&self, inode: Arc<dyn Inode>) -> Result<Arc<dyn Filesystem>> {
self.state
.lock_save_irq()
Expand Down
17 changes: 17 additions & 0 deletions src/fs/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};
use alloc::{boxed::Box, sync::Arc};
use async_trait::async_trait;
use core::any::Any;
use core::{
future,
pin::pin,
Expand Down Expand Up @@ -115,6 +116,14 @@ impl PipeReader {

#[async_trait]
impl FileOps for PipeReader {
fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

async fn read(&mut self, _ctx: &mut FileCtx, u_buf: UA, count: usize) -> Result<usize> {
self.readat(u_buf, count, 0).await
}
Expand Down Expand Up @@ -194,6 +203,14 @@ impl PipeWriter {

#[async_trait]
impl FileOps for PipeWriter {
fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

async fn read(&mut self, _ctx: &mut FileCtx, _buf: UA, _count: usize) -> Result<usize> {
Err(KernelError::BadFd)
}
Expand Down
9 changes: 9 additions & 0 deletions src/fs/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
};
use alloc::{boxed::Box, sync::Arc};
use async_trait::async_trait;
use core::any::Any;
use core::{cmp::min, pin::Pin};
use libkernel::{
error::Result,
Expand All @@ -29,6 +30,14 @@ impl RegFile {

#[async_trait]
impl FileOps for RegFile {
fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

/// Reads data from the current file position into `buf`. The file's cursor
/// is advanced by the number of bytes read.
async fn readat(
Expand Down
Loading
Loading