Skip to content
Closed
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
33 changes: 32 additions & 1 deletion src/arch/arm64/exceptions/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ use crate::{
brk::sys_brk,
mmap::{sys_mmap, sys_mprotect, sys_munmap},
},
net::syscalls::{
recv::sys_recvfrom, send::sys_sendto, sys_accept, sys_bind, sys_connect, sys_listen,
sys_shutdown, sys_socket,
},
process::{
clone::sys_clone,
creds::{
Expand Down Expand Up @@ -348,7 +352,34 @@ pub async fn handle_syscall() {
0xb1 => sys_getegid().map_err(|e| match e {}),
0xb2 => sys_gettid().map_err(|e| match e {}),
0xb3 => sys_sysinfo(TUA::from_value(arg1 as _)).await,
0xc6 => Err(KernelError::NotSupported),
0xc6 => sys_socket(arg1 as _, arg2 as _, arg3 as _).await,
0xc8 => sys_bind(arg1.into(), TUA::from_value(arg2 as _), arg3 as _).await,
0xc9 => sys_listen(arg1.into(), arg2 as _).await,
0xca => sys_accept(arg1.into(), TUA::from_value(arg2 as _), arg3 as _).await,
0xcb => sys_connect(arg1.into(), TUA::from_value(arg2 as _), arg3 as _).await,
0xce => {
sys_sendto(
arg1.into(),
TUA::from_value(arg2 as _),
arg3 as _,
arg4 as _,
TUA::from_value(arg5 as _),
arg6 as _,
)
.await
}
0xcf => {
sys_recvfrom(
arg1.into(),
TUA::from_value(arg2 as _),
arg3 as _,
arg4 as _,
TUA::from_value(arg5 as _),
arg6 as _,
)
.await
}
0xd2 => sys_shutdown(arg1.into(), arg2 as _).await,
0xd6 => sys_brk(VA::from_value(arg1 as _))
.await
.map_err(|e| match e {}),
Expand Down
2 changes: 1 addition & 1 deletion src/console/chardev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl OpenableDevice for TtyDev {
Ok(task
.fd_table
.lock_save_irq()
.get(Fd(0))
.get_file(Fd(0))
.ok_or(FsError::NoDevice)?)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub async fn sys_getdents64(fd: Fd, mut ubuf: UA, size: u32) -> Result<usize> {
let file = task
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let (ops, ctx) = &mut *file.lock().await;
Expand Down
2 changes: 1 addition & 1 deletion src/fs/syscalls/at/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
let file = task
.fd_table
.lock_save_irq()
.get(dirfd)
.get_file(dirfd)
.ok_or(KernelError::BadFd)?;

let inode = file.inode().ok_or(KernelError::NotSupported)?;
Expand Down Expand Up @@ -83,7 +83,7 @@
.get(dirfd)
.ok_or(KernelError::BadFd)?;

file.inode().ok_or(KernelError::NotSupported)?

Check failure on line 86 in src/fs/syscalls/at/mod.rs

View workflow job for this annotation

GitHub Actions / build-test

no method named `inode` found for enum `process::fd_table::FileDescriptorEntryItem` in the current scope
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/fs/syscalls/chdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub async fn sys_fchdir(fd: Fd) -> Result<usize> {
let file = task
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

*task.cwd.lock_save_irq() = (
Expand Down
23 changes: 16 additions & 7 deletions src/fs/syscalls/close.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use crate::process::fd_table::FileDescriptorEntryItem;
use crate::{process::fd_table::Fd, sched::current_task};
use alloc::sync::Arc;
use libkernel::error::{KernelError, Result};

pub async fn sys_close(fd: Fd) -> Result<usize> {
let file = current_task()
let fd_item = current_task()
.fd_table
.lock_save_irq()
.remove(fd)
.ok_or(KernelError::BadFd)?;

if let Some(file) = Arc::into_inner(file) {
let (ops, ctx) = &mut *file.lock().await;
ops.release(ctx).await?;
match fd_item {
FileDescriptorEntryItem::File(file) => {
if let Some(file) = Arc::into_inner(file) {
let (ops, ctx) = &mut *file.lock().await;
ops.release(ctx).await?;

Ok(0)
} else {
Ok(0)
Ok(0)
} else {
Ok(0)
}
}
FileDescriptorEntryItem::Socket(socket) => {
todo!();
Ok(0)

Check failure on line 26 in src/fs/syscalls/close.rs

View workflow job for this annotation

GitHub Actions / build-test

unreachable expression
}
}
}
2 changes: 1 addition & 1 deletion src/fs/syscalls/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub async fn sys_ioctl(fd: Fd, request: usize, arg: usize) -> Result<usize> {
let fd = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let (ops, ctx) = &mut *fd.lock().await;
Expand Down
4 changes: 2 additions & 2 deletions src/fs/syscalls/iov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
let file = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let iovs = copy_obj_array_from_user(iov_ptr, no_iov).await?;
Expand All @@ -36,7 +36,7 @@
let file = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let iovs = copy_obj_array_from_user(iov_ptr, no_iov).await?;
Expand Down Expand Up @@ -69,9 +69,9 @@

let iovs = copy_obj_array_from_user(iov_ptr, no_iov).await?;

let (ops, _state) = &mut *file.lock().await;

Check failure on line 72 in src/fs/syscalls/iov.rs

View workflow job for this annotation

GitHub Actions / build-test

no method named `lock` found for enum `process::fd_table::FileDescriptorEntryItem` in the current scope

ops.writevat(&iovs, offset).await

Check failure on line 74 in src/fs/syscalls/iov.rs

View workflow job for this annotation

GitHub Actions / build-test

type annotations needed
}

pub async fn sys_preadv2(
Expand All @@ -89,7 +89,7 @@

let iovs = copy_obj_array_from_user(iov_ptr, no_iov).await?;

let (ops, _state) = &mut *file.lock().await;

Check failure on line 92 in src/fs/syscalls/iov.rs

View workflow job for this annotation

GitHub Actions / build-test

no method named `lock` found for enum `process::fd_table::FileDescriptorEntryItem` in the current scope

ops.readvat(&iovs, offset).await

Check failure on line 94 in src/fs/syscalls/iov.rs

View workflow job for this annotation

GitHub Actions / build-test

type annotations needed
}
4 changes: 2 additions & 2 deletions src/fs/syscalls/rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let file = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let (ops, ctx) = &mut *file.lock().await;
Expand All @@ -20,7 +20,7 @@
let file = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let (ops, ctx) = &mut *file.lock().await;
Expand All @@ -35,7 +35,7 @@
.get(fd)
.ok_or(KernelError::BadFd)?;

let (ops, _ctx) = &mut *file.lock().await;

Check failure on line 38 in src/fs/syscalls/rw.rs

View workflow job for this annotation

GitHub Actions / build-test

no method named `lock` found for enum `process::fd_table::FileDescriptorEntryItem` in the current scope

ops.writeat(user_buf, count, offset).await
}
Expand Down
2 changes: 1 addition & 1 deletion src/fs/syscalls/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn sys_lseek(fd: Fd, offset: isize, whence: i32) -> Result<usize> {
let fd = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let (ops, ctx) = &mut *fd.lock().await;
Expand Down
4 changes: 2 additions & 2 deletions src/fs/syscalls/splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub async fn sys_sendfile(
let task = current_task();
let fds = task.fd_table.lock_save_irq();

let reader = fds.get(in_fd).ok_or(KernelError::BadFd)?;
let writer = fds.get(out_fd).ok_or(KernelError::BadFd)?;
let reader = fds.get_file(in_fd).ok_or(KernelError::BadFd)?;
let writer = fds.get_file(out_fd).ok_or(KernelError::BadFd)?;

(reader, writer)
};
Expand Down
2 changes: 1 addition & 1 deletion src/fs/syscalls/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub async fn sys_fstat(fd: Fd, statbuf: TUA<Stat>) -> Result<usize> {
let fd = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let inode = fd.inode().ok_or(KernelError::BadFd)?;
Expand Down
2 changes: 1 addition & 1 deletion src/fs/syscalls/trunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn sys_ftruncate(fd: Fd, new_size: usize) -> Result<usize> {
let fd = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let (ops, ctx) = &mut *fd.lock().await;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod fs;
mod interrupts;
mod kernel;
mod memory;
mod net;
mod process;
mod sched;
mod sync;
Expand Down
2 changes: 1 addition & 1 deletion src/memory/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub async fn sys_mmap(
let fd = current_task()
.fd_table
.lock_save_irq()
.get(fd)
.get_file(fd)
.ok_or(KernelError::BadFd)?;

let inode = fd.inode().ok_or(KernelError::BadFd)?;
Expand Down
3 changes: 3 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod open_socket;
mod sops;
pub mod syscalls;
27 changes: 27 additions & 0 deletions src/net/open_socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::net::sops::SocketOps;
use crate::sync::{AsyncMutexGuard, Mutex};
use alloc::boxed::Box;

pub struct SocketCtx {}

impl SocketCtx {
pub fn new() -> Self {
Self {}
}
}

pub struct OpenSocket {
state: Mutex<(Box<dyn SocketOps>, SocketCtx)>,
}

impl OpenSocket {
pub fn new(ops: Box<dyn SocketOps>) -> Self {
Self {
state: Mutex::new((ops, SocketCtx::new())),
}
}

pub async fn lock(&self) -> AsyncMutexGuard<'_, (Box<dyn SocketOps>, SocketCtx)> {
self.state.lock().await
}
}
33 changes: 33 additions & 0 deletions src/net/sops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::net::open_socket::SocketCtx;
use alloc::boxed::Box;
use alloc::vec::Vec;
use async_trait::async_trait;
use libkernel::error::{KernelError, Result};

#[async_trait]
pub trait SocketOps: Send + Sync {
async fn bind(&self, _ctx: &mut SocketCtx, _addr: &[u8]) -> Result<()> {
Err(KernelError::NotSupported)
}
async fn connect(&self, _ctx: &mut SocketCtx, _addr: &[u8]) -> Result<()> {
Err(KernelError::NotSupported)
}
async fn listen(&self, _ctx: &mut SocketCtx, _backlog: i32) -> Result<()> {
Err(KernelError::NotSupported)
}
async fn accept(&self, _ctx: &mut SocketCtx) -> Result<Box<dyn SocketOps>> {
Err(KernelError::NotSupported)
}
async fn sendmsg(&self, _ctx: &mut SocketCtx, _msg: &[u8]) -> Result<usize> {
Err(KernelError::NotSupported)
}
async fn recvmsg(&self, _ctx: &mut SocketCtx, _buf: &mut [u8]) -> Result<usize> {
Err(KernelError::NotSupported)
}
fn getsockopt(&self, _ctx: &mut SocketCtx, _level: i32, _opt: i32) -> Result<Vec<u8>> {
Err(KernelError::NotSupported)
}
fn setsockopt(&self, _ctx: &mut SocketCtx, _level: i32, _opt: i32, _val: &[u8]) -> Result<()> {
Err(KernelError::NotSupported)
}
}
113 changes: 113 additions & 0 deletions src/net/syscalls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use crate::process::fd_table::Fd;
use crate::sched::current_task;
use libkernel::error::KernelError;
use libkernel::memory::address::UA;

pub mod recv;
pub mod send;

pub enum AddressFamily {
Unix = 1,
Inet = 2,
Inet6 = 10,
}

impl TryFrom<i32> for AddressFamily {
type Error = KernelError;

fn try_from(value: i32) -> libkernel::error::Result<Self> {
match value {
1 => Ok(AddressFamily::Unix),
2 => Ok(AddressFamily::Inet),
10 => Ok(AddressFamily::Inet6),
_ => Err(KernelError::InvalidValue),
}
}
}

pub enum SocketType {
Datagram = 1,
Stream = 2,
Raw = 3,
RDM = 4,
SeqPacket = 5,
DCCP = 6,
Packet = 10,
}

impl TryFrom<i32> for SocketType {
type Error = KernelError;

fn try_from(value: i32) -> libkernel::error::Result<Self> {
match value {
1 => Ok(SocketType::Datagram),
2 => Ok(SocketType::Stream),
3 => Ok(SocketType::Raw),
4 => Ok(SocketType::RDM),
5 => Ok(SocketType::SeqPacket),
6 => Ok(SocketType::DCCP),
10 => Ok(SocketType::Packet),
_ => Err(KernelError::InvalidValue),
}
}
}

pub async fn sys_socket(
domain: i32,
type_: i32,
_protocol: i32,
) -> libkernel::error::Result<usize> {
let _family = AddressFamily::try_from(domain)?;
// TODO: mask out flags from type_
let _socket_type = SocketType::try_from(type_)?;
Err(KernelError::NotSupported)
}

pub async fn sys_bind(fd: Fd, addr: UA, addr_len: u32) -> libkernel::error::Result<usize> {
Err(KernelError::NotSupported)
}

pub async fn sys_listen(fd: Fd, backlog: i32) -> libkernel::error::Result<usize> {
let task = current_task();
let socket = task
.fd_table
.lock_save_irq()
.get_socket(fd)
.ok_or(KernelError::BadFd)?;
let (ops, state) = &mut *socket.lock().await;
ops.listen(state, backlog).await?;
Ok(0)
}

pub async fn sys_accept(fd: Fd, addr: UA, addr_len: u32) -> libkernel::error::Result<usize> {
let _ = (addr, addr_len);
Err(KernelError::NotSupported)
}

pub async fn sys_connect(fd: Fd, addr: UA, addr_len: u32) -> libkernel::error::Result<usize> {
Err(KernelError::NotSupported)
}

pub enum ShutdownHow {
Read = 0,
Write = 1,
Both = 2,
}

impl TryFrom<i32> for ShutdownHow {
type Error = KernelError;

fn try_from(value: i32) -> libkernel::error::Result<Self> {
match value {
0 => Ok(ShutdownHow::Read),
1 => Ok(ShutdownHow::Write),
2 => Ok(ShutdownHow::Both),
_ => Err(KernelError::InvalidValue),
}
}
}

pub async fn sys_shutdown(fd: Fd, how: i32) -> libkernel::error::Result<usize> {
let _how = ShutdownHow::try_from(how)?;
Err(KernelError::NotSupported)
}
Loading
Loading