From 8bc96e901ebc3973af752236a18f897251f30e37 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 10 Nov 2024 07:06:07 +0900 Subject: [PATCH] sys: Add arm_compat::sys_read_uninit and mips::{mips_read_uninit,mips_pread_uninit} --- src/sys/arm_compat/mod.rs | 17 +++++++++++------ src/sys/mips/mod.rs | 25 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/sys/arm_compat/mod.rs b/src/sys/arm_compat/mod.rs index f012e33..1451ec6 100644 --- a/src/sys/arm_compat/mod.rs +++ b/src/sys/arm_compat/mod.rs @@ -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}, @@ -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]) -> Result { +pub fn sys_read_uninit(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit]) -> Result { 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)) }; @@ -308,15 +309,19 @@ pub fn sys_read(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit]) -> Result, buf: &mut [u8]) -> Result { - use core::slice; - +pub(crate) fn sys_read_init(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result { let len = buf.len(); // SAFETY: transmuting initialized u8 to MaybeUninit is always safe. let buf = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::>(), 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 { diff --git a/src/sys/mips/mod.rs b/src/sys/mips/mod.rs index 5b6abe2..04af7a7 100644 --- a/src/sys/mips/mod.rs +++ b/src/sys/mips/mod.rs @@ -15,7 +15,8 @@ pub mod syscall; use core::{ ffi::{self, CStr}, - mem, + mem::{self, MaybeUninit}, + slice, }; use self::syscall::{ @@ -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 { +pub fn mips_read_uninit(fd: BorrowedFd<'_>, buf: &mut [MaybeUninit]) -> Result { let len = buf.len(); let (res, errno) = unsafe { syscall3( @@ -167,6 +168,14 @@ pub fn mips_read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result { Ok(res.usize()) } } +pub fn mips_read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> Result { + let len = buf.len(); + // SAFETY: transmuting initialized u8 to MaybeUninit is always safe. + let buf = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::>(), 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; @@ -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 { +pub fn mips_pread_uninit( + fd: BorrowedFd<'_>, + buf: &mut [MaybeUninit], + offset: usize, +) -> Result { let len = buf.len(); let (res, errno) = unsafe { syscall4( @@ -279,6 +292,12 @@ pub fn mips_pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: usize) -> Result, buf: &mut [u8], offset: usize) -> Result { + let len = buf.len(); + // SAFETY: transmuting initialized u8 to MaybeUninit is always safe. + let buf = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast::>(), len) }; + mips_pread_uninit(fd, buf, offset) +} pub fn mips_pwrite(fd: BorrowedFd<'_>, buf: &[u8], offset: usize) -> Result { let (res, errno) = unsafe {