Skip to content

Commit

Permalink
sys: Add arm_compat::sys_read_uninit and mips::{mips_read_uninit,mips…
Browse files Browse the repository at this point in the history
…_pread_uninit}
  • Loading branch information
taiki-e committed Nov 9, 2024
1 parent 59e761b commit 8bc96e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/sys/arm_compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub(crate) mod errno;
pub(crate) mod fs;
pub mod syscall;

#[cfg(any(feature = "stdio", feature = "fs"))]
use core::slice;
use core::{
ffi::{c_void, CStr},
mem::{self, MaybeUninit},
Expand Down Expand Up @@ -296,9 +298,8 @@ pub(crate) fn should_close(_fd: &OwnedFd) -> bool {
true
}

// TODO: Add read_uninit?
/// [SYS_READ (0x06)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-read-0x06)
pub fn sys_read(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit<u8>]) -> Result<usize> {
pub fn sys_read_uninit(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit<u8>]) -> Result<usize> {
let len = buf.len();
let mut args = [ParamRegW::fd(fd), ParamRegW::buf(buf), ParamRegW::usize(len)];
let res = unsafe { syscall(OperationNumber::SYS_READ, ParamRegW::block(&mut args)) };
Expand All @@ -308,15 +309,19 @@ pub fn sys_read(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit<u8>]) -> Result<usize
Err(Error::from_raw_os_error(sys_errno()))
}
}
// TODO: make sys_read take initialized buf in the next breaking release (e.i., rename sys_read_init to sys_read and make it public).
pub use sys_read_uninit as sys_read;
#[cfg(any(feature = "stdio", feature = "fs"))]
pub(crate) fn read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result<usize> {
use core::slice;

pub(crate) fn sys_read_init(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result<usize> {
let len = buf.len();
// SAFETY: transmuting initialized u8 to MaybeUninit<u8> is always safe.
let buf = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<MaybeUninit<u8>>(), len) };
sys_read(fd, buf)
sys_read_uninit(fd, buf)
}
// #[cfg(any(feature = "stdio", feature = "fs"))]
// pub(crate) use self::sys_read_uninit as read_uninit;
#[cfg(any(feature = "stdio", feature = "fs"))]
pub(crate) use self::sys_read_init as read;

/// [SYS_READC (0x07)](https://github.com/ARM-software/abi-aa/blob/2024Q3/semihosting/semihosting.rst#sys-readc-0x07)
pub fn sys_readc() -> u8 {
Expand Down
25 changes: 22 additions & 3 deletions src/sys/mips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub mod syscall;

use core::{
ffi::{self, CStr},
mem,
mem::{self, MaybeUninit},
slice,
};

use self::syscall::{
Expand Down Expand Up @@ -150,7 +151,7 @@ pub unsafe fn mips_close(fd: RawFd) -> Result<()> {
}
pub(crate) use self::mips_close as close;

pub fn mips_read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result<usize> {
pub fn mips_read_uninit(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit<u8>]) -> Result<usize> {
let len = buf.len();
let (res, errno) = unsafe {
syscall3(
Expand All @@ -167,6 +168,14 @@ pub fn mips_read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result<usize> {
Ok(res.usize())
}
}
pub fn mips_read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result<usize> {
let len = buf.len();
// SAFETY: transmuting initialized u8 to MaybeUninit<u8> is always safe.
let buf = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<MaybeUninit<u8>>(), len) };
mips_read_uninit(fd, buf)
}
// #[cfg(any(feature = "stdio", feature = "fs"))]
// pub(crate) use mips_read_uninit as read_uninit;
#[cfg(any(feature = "stdio", feature = "fs"))]
pub(crate) use self::mips_read as read;

Expand Down Expand Up @@ -261,7 +270,11 @@ pub unsafe fn mips_argn(n: usize, buf: *mut u8) -> Result<()> {
// TODO: UHI_PLOG
// TODO: UHI_ASSERT

pub fn mips_pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: usize) -> Result<usize> {
pub fn mips_pread_uninit(
fd: BorrowedFd<'_>,
buf: &mut [MaybeUninit<u8>],
offset: usize,
) -> Result<usize> {
let len = buf.len();
let (res, errno) = unsafe {
syscall4(
Expand All @@ -279,6 +292,12 @@ pub fn mips_pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: usize) -> Result<u
Ok(res.usize())
}
}
pub fn mips_pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: usize) -> Result<usize> {
let len = buf.len();
// SAFETY: transmuting initialized u8 to MaybeUninit<u8> is always safe.
let buf = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<MaybeUninit<u8>>(), len) };
mips_pread_uninit(fd, buf, offset)
}

pub fn mips_pwrite(fd: BorrowedFd<'_>, buf: &[u8], offset: usize) -> Result<usize> {
let (res, errno) = unsafe {
Expand Down

0 comments on commit 8bc96e9

Please sign in to comment.