Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ time = { version = "0.3", default-features = false }
volatile = "0.6"
zerocopy = { version = "0.8", default-features = false }
uhyve-interface = "0.1.3"
bitfield-struct = "0.11.0"

[dependencies.smoltcp]
version = "0.12"
Expand Down
9 changes: 9 additions & 0 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ pub(crate) trait ObjectInterface: Sync + Send + core::fmt::Debug {
Err(Errno::Nosys)
}

/// Handles an ioctl
fn handle_ioctl(
&self,
_cmd: crate::fs::ioctl::IoCtlCall,
_argp: *mut core::ffi::c_void,
) -> io::Result<()> {
Err(Errno::Nosys)
}

/// `isatty` returns `true` for a terminal device
async fn isatty(&self) -> io::Result<bool> {
Ok(false)
Expand Down
27 changes: 27 additions & 0 deletions src/fd/socket/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
use crate::errno::Errno;
use crate::executor::block_on;
use crate::fd::ObjectInterface;
use crate::{fd, io};

#[cfg(feature = "tcp")]
pub(crate) mod tcp;
#[cfg(feature = "udp")]
pub(crate) mod udp;
#[cfg(feature = "vsock")]
pub(crate) mod vsock;

/// Handles an ioctl (general function)
fn socket_handle_ioctl(
this: &dyn ObjectInterface,
cmd: crate::fs::ioctl::IoCtlCall,
argp: *mut core::ffi::c_void,
) -> io::Result<()> {
const FIONBIO: u32 = 0x8008_667eu32;

if cmd.into_bits() == FIONBIO {
let value = unsafe { *(argp as *const i32) };
let status_flags = if value != 0 {
fd::StatusFlags::O_NONBLOCK
} else {
fd::StatusFlags::empty()
};

block_on(this.set_status_flags(status_flags), None)
} else {
Err(Errno::Inval)
}
}
5 changes: 5 additions & 0 deletions src/fd/socket/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::boxed::Box;
use alloc::collections::BTreeSet;
use alloc::sync::Arc;
use core::ffi::c_void;
use core::future;
use core::mem::MaybeUninit;
use core::sync::atomic::{AtomicU16, Ordering};
Expand Down Expand Up @@ -533,4 +534,8 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.write().await.set_status_flags(status_flags).await
}

fn handle_ioctl(&self, cmd: crate::fs::ioctl::IoCtlCall, argp: *mut c_void) -> io::Result<()> {
super::socket_handle_ioctl(self, cmd, argp)
}
}
5 changes: 5 additions & 0 deletions src/fd/socket/udp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::boxed::Box;
use core::ffi::c_void;
use core::future;
use core::mem::MaybeUninit;
use core::task::Poll;
Expand Down Expand Up @@ -293,4 +294,8 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.write().await.set_status_flags(status_flags).await
}

fn handle_ioctl(&self, cmd: crate::fs::ioctl::IoCtlCall, argp: *mut c_void) -> io::Result<()> {
super::socket_handle_ioctl(self, cmd, argp)
}
}
5 changes: 5 additions & 0 deletions src/fd/socket/vsock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::ffi::c_void;
use core::future;
use core::mem::MaybeUninit;
use core::task::Poll;
Expand Down Expand Up @@ -471,4 +472,8 @@ impl ObjectInterface for async_lock::RwLock<Socket> {
async fn set_status_flags(&self, status_flags: fd::StatusFlags) -> io::Result<()> {
self.write().await.set_status_flags(status_flags).await
}

fn handle_ioctl(&self, cmd: crate::fs::ioctl::IoCtlCall, argp: *mut c_void) -> io::Result<()> {
super::socket_handle_ioctl(self, cmd, argp)
}
}
135 changes: 135 additions & 0 deletions src/fs/ioctl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//! A module for custom IOCTL objects

use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt::Debug;

use bitfield_struct::bitfield;

use crate::fd::{AccessPermission, ObjectInterface};
use crate::fs::{NodeKind, VfsNode};
use crate::io;

/// Encoding for an IOCTL command, as done in the Linux Kernel.
///
/// See [relevant kernel header](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/asm-generic/ioctl.h?h=v6.15) for reference.
///
/// The goal of this interface is to easily support linux applications that communicate via IOCTL,
/// so linux compatibility is an intended and explicit goal here.
#[bitfield(u32)]
pub struct IoCtlCall {
call_nr: u8,

call_type: u8,

#[bits(2, from = IoCtlDirection::from_bits_truncate, default = IoCtlDirection::empty())]
call_dir: IoCtlDirection,

#[bits(14)]
call_size: u16,
}

bitflags! {
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub struct IoCtlDirection: u8 {
const IOC_WRITE = 1;
const IOC_READ = 2;
}
}

impl IoCtlDirection {
// Required for IoCtlCall
const fn into_bits(self) -> u8 {
self.bits()
}
}

#[derive(Debug)]
struct IoCtlNode(Arc<dyn ObjectInterface>);

impl VfsNode for IoCtlNode {
fn get_kind(&self) -> NodeKind {
NodeKind::File
}

fn get_object(&self) -> io::Result<Arc<dyn ObjectInterface>> {
Ok(self.0.clone())
}
}

/// Register a custom object to handle IOCTLS at a given path,
///
/// This call mounts a trivial VfsNode that opens to the provided ioctl_object at the given path.
#[allow(dead_code)]
pub(crate) fn register_ioctl(path: &str, ioctl_object: Arc<dyn ObjectInterface>) {
assert!(path.starts_with("/"));
let mut path: Vec<&str> = path.split("/").skip(1).collect();
assert!(!path.is_empty());

let fs = super::FILESYSTEM
.get()
.expect("Failed to mount ioctl: filesystem is not yet initialized");

// Create parent directory
let mut directory: Vec<&str> = path.clone();
directory.pop().unwrap(); // remove file name
directory.reverse();

if !directory.is_empty() {
let _ = fs
.root
.traverse_mkdir(&mut directory, AccessPermission::all()); // ignore possible errors at this step
}

// Mount the file
path.reverse();
fs.root
.traverse_mount(&mut path, Box::new(IoCtlNode(ioctl_object)))
.expect("Failed to mount ioctl: filesystem error");
}

#[cfg(all(test, not(target_os = "none")))]
mod tests {
use super::{IoCtlCall, IoCtlDirection};

#[test]
fn ioctl_call_correctly_written() {
let call_nr = 0x12u8;
let call_type = 0x78u8;
let call_dir = IoCtlDirection::IOC_WRITE;
let call_size = 0x423u16;

let ioctl_call_number = (u32::from(call_size) << 18)
| (u32::from(call_dir.bits()) << 16)
| (u32::from(call_type) << 8)
| u32::from(call_nr);

let call = IoCtlCall::new()
.with_call_nr(call_nr)
.with_call_type(call_type)
.with_call_dir(call_dir)
.with_call_size(call_size);

assert_eq!(ioctl_call_number, call.into_bits());
}
#[test]
fn ioctl_call_correctly_parsed() {
let call_nr = 0x12u8;
let call_type = 0x78u8;
let call_dir = IoCtlDirection::IOC_WRITE;
let call_size = 0x423u16;

let ioctl_call_number = (u32::from(call_size) << 18)
| (u32::from(call_dir.bits()) << 16)
| (u32::from(call_type) << 8)
| u32::from(call_nr);

let parsed = IoCtlCall::from_bits(ioctl_call_number);

assert_eq!(call_nr, parsed.call_nr());
assert_eq!(call_type, parsed.call_type());
assert_eq!(call_dir, parsed.call_dir());
assert_eq!(call_size, parsed.call_size());
}
}
1 change: 1 addition & 0 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(all(feature = "fuse", feature = "pci"))]
pub(crate) mod fuse;
pub mod ioctl;
mod mem;
mod uhyve;

Expand Down
34 changes: 11 additions & 23 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use alloc::ffi::CString;
#[cfg(all(target_os = "none", not(feature = "common-os")))]
use core::alloc::{GlobalAlloc, Layout};
use core::ffi::{CStr, c_char};
use core::ffi::{CStr, c_char, c_ulong};
use core::marker::PhantomData;
use core::ptr::null;

Expand All @@ -28,6 +28,7 @@ use crate::fd::{
self, AccessOption, AccessPermission, EventFlags, FileDescriptor, OpenOption, PollFd,
dup_object, dup_object2, get_object, isatty, remove_object,
};
use crate::fs::ioctl::IoCtlCall;
use crate::fs::{self, FileAttr, SeekWhence};
#[cfg(all(target_os = "none", not(feature = "common-os")))]
use crate::mm::ALLOCATOR;
Expand Down Expand Up @@ -624,30 +625,17 @@ pub unsafe extern "C" fn sys_writev(fd: FileDescriptor, iov: *const iovec, iovcn
#[unsafe(no_mangle)]
pub unsafe extern "C" fn sys_ioctl(
fd: FileDescriptor,
cmd: i32,
cmd: c_ulong,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if we can remove this change to preserve the ABI

argp: *mut core::ffi::c_void,
) -> i32 {
const FIONBIO: i32 = 0x8008_667eu32 as i32;

if cmd == FIONBIO {
let value = unsafe { *(argp as *const i32) };
let status_flags = if value != 0 {
fd::StatusFlags::O_NONBLOCK
} else {
fd::StatusFlags::empty()
};

let obj = get_object(fd);
obj.map_or_else(
|e| -i32::from(e),
|v| {
block_on((*v).set_status_flags(status_flags), None)
.map_or_else(|e| -i32::from(e), |()| 0)
},
)
} else {
-i32::from(Errno::Inval)
}
let obj = get_object(fd);
obj.map_or_else(
|e| -i32::from(e),
|v| {
(*v).handle_ioctl(IoCtlCall::from_bits(cmd as u32), argp)
.map_or_else(|e| -i32::from(e), |()| 0)
},
)
}

/// manipulate file descriptor
Expand Down