Skip to content

Commit

Permalink
add system calls readv and writev
Browse files Browse the repository at this point in the history
the offers the possibility to read or to write data into multiple
buffers
  • Loading branch information
stlankes committed May 21, 2024
1 parent 20d6c06 commit 17edb13
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ pub(crate) static SYS: Lazy<&'static dyn SyscallInterface> = Lazy::new(|| {
}
});

#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct iovec {
/// Starting address
pub iov_base: *mut u8,
/// Size of the memory pointed to by iov_base.
pub iov_len: usize,
}

pub(crate) fn init() {
Lazy::force(&SYS);

Expand Down Expand Up @@ -404,6 +413,33 @@ pub unsafe extern "C" fn sys_read(fd: FileDescriptor, buf: *mut u8, len: usize)
)
}

#[hermit_macro::system]
pub unsafe extern "C" fn sys_readv(fd: i32, iov: *const iovec, iovcnt: usize) -> isize {
let mut count: isize = 0;
let slice = unsafe { core::slice::from_raw_parts(iov, iovcnt) };

for i in slice {
let buf = unsafe { core::slice::from_raw_parts_mut(i.iov_base, i.iov_len) };

let len = crate::fd::read(fd, buf).map_or_else(
|e| -num::ToPrimitive::to_isize(&e).unwrap(),
|v| v.try_into().unwrap(),
);

if len < 0 {
return len;
}

count += len;

if len < i.iov_len.try_into().unwrap() {
return count;
}
}

count
}

unsafe fn write(fd: FileDescriptor, buf: *const u8, len: usize) -> isize {
let slice = unsafe { core::slice::from_raw_parts(buf, len) };
crate::fd::write(fd, slice).map_or_else(
Expand All @@ -417,6 +453,33 @@ pub unsafe extern "C" fn sys_write(fd: FileDescriptor, buf: *const u8, len: usiz
unsafe { write(fd, buf, len) }
}

#[hermit_macro::system]
pub unsafe extern "C" fn sys_writev(fd: FileDescriptor, iov: *const iovec, iovcnt: usize) -> isize {
let mut count: isize = 0;
let slice = unsafe { core::slice::from_raw_parts(iov, iovcnt) };

for i in slice {
let buf = unsafe { core::slice::from_raw_parts(i.iov_base, i.iov_len) };

let len = crate::fd::write(fd, buf).map_or_else(
|e| -num::ToPrimitive::to_isize(&e).unwrap(),
|v| v.try_into().unwrap(),
);

if len < 0 {
return len;
}

count += len;

if len < i.iov_len.try_into().unwrap() {
return count;
}
}

count
}

#[hermit_macro::system]
pub unsafe extern "C" fn sys_ioctl(
fd: FileDescriptor,
Expand Down

0 comments on commit 17edb13

Please sign in to comment.