From 59db13756e164ca47a8c26d1efbb78349963c1c3 Mon Sep 17 00:00:00 2001 From: pjht Date: Thu, 3 Oct 2024 12:20:48 -0500 Subject: [PATCH] mikros: Add errno --- library/std/src/io/error.rs | 8 +- library/std/src/os/mikros/errno.rs | 423 +++++++++++++++++++++++ library/std/src/os/mikros/mod.rs | 5 + library/std/src/sys/pal/mikros/common.rs | 51 ++- library/std/src/sys/pal/mikros/os.rs | 9 +- library/std/src/sys/pal/mod.rs | 2 +- 6 files changed, 485 insertions(+), 13 deletions(-) create mode 100644 library/std/src/os/mikros/errno.rs diff --git a/library/std/src/io/error.rs b/library/std/src/io/error.rs index 6ecd9469c17..540dbf7d862 100644 --- a/library/std/src/io/error.rs +++ b/library/std/src/io/error.rs @@ -1,14 +1,14 @@ #[cfg(test)] mod tests; -#[cfg(all(target_pointer_width = "64", not(target_os = "uefi")))] +#[cfg(all(target_pointer_width = "64", not(any(target_os = "uefi", target_os = "mikros"))))] mod repr_bitpacked; -#[cfg(all(target_pointer_width = "64", not(target_os = "uefi")))] +#[cfg(all(target_pointer_width = "64", not(any(target_os = "uefi", target_os = "mikros"))))] use repr_bitpacked::Repr; -#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))] +#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi", target_os = "mikros"))] mod repr_unpacked; -#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi"))] +#[cfg(any(not(target_pointer_width = "64"), target_os = "uefi", target_os = "mikros"))] use repr_unpacked::Repr; use crate::{error, fmt, result, sys}; diff --git a/library/std/src/os/mikros/errno.rs b/library/std/src/os/mikros/errno.rs new file mode 100644 index 00000000000..9a7079b2e5d --- /dev/null +++ b/library/std/src/os/mikros/errno.rs @@ -0,0 +1,423 @@ +#[stable(feature = "mikros", since = "1.80.0")] +#[repr(i32)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum Errno { + EPERM = 1, + ENOENT = 2, + ESRCH = 3, + EINTR = 4, + EIO = 5, + ENXIO = 6, + E2BIG = 7, + ENOEXEC = 8, + EBADF = 9, + ECHILD = 10, + EWOULDBLOCK = 11, + ENOMEM = 12, + EACCES = 13, + EFAULT = 14, + ENOTBLK = 15, + EBUSY = 16, + EEXIST = 17, + EXDEV = 18, + ENODEV = 19, + ENOTDIR = 20, + EISDIR = 21, + EINVAL = 22, + ENFILE = 23, + EMFILE = 24, + ENOTTY = 25, + ETXTBSY = 26, + EFBIG = 27, + ENOSPC = 28, + ESPIPE = 29, + EROFS = 30, + EMLINK = 31, + EPIPE = 32, + EDOM = 33, + ERANGE = 34, + EDEADLK = 35, + ENAMETOOLONG = 36, + ENOLCK = 37, + ENOSYS = 38, + ENOTEMPTY = 39, + ELOOP = 40, + ENOMSG = 42, + EIDRM = 43, + ECHRNG = 44, + EL2NSYNC = 45, + EL3HLT = 46, + EL3RST = 47, + ELNRNG = 48, + EUNATCH = 49, + ENOCSI = 50, + EL2HLT = 51, + EBADE = 52, + EBADR = 53, + EXFULL = 54, + ENOANO = 55, + EBADRQC = 56, + EBADSLT = 57, + EBFONT = 59, + ENOSTR = 60, + ENODATA = 61, + ETIME = 62, + ENOSR = 63, + ENONET = 64, + ENOPKG = 65, + EREMOTE = 66, + ENOLINK = 67, + EADV = 68, + ESRMNT = 69, + ECOMM = 70, + EPROTO = 71, + EMULTIHOP = 72, + EDOTDOT = 73, + EBADMSG = 74, + EOVERFLOW = 75, + ENOTUNIQ = 76, + EBADFD = 77, + EREMCHG = 78, + ELIBACC = 79, + ELIBBAD = 80, + ELIBSCN = 81, + ELIBMAX = 82, + ELIBEXEC = 83, + EILSEQ = 84, + ERESTART = 85, + ESTRPIPE = 86, + EUSERS = 87, + ENOTSOCK = 88, + EDESTADDRREQ = 89, + EMSGSIZE = 90, + EPROTOTYPE = 91, + ENOPROTOOPT = 92, + EPROTONOSUPPORT = 93, + ESOCKTNOSUPPORT = 94, + ENOTSUP = 95, + EPFNOSUPPORT = 96, + EAFNOSUPPORT = 97, + EADDRINUSE = 98, + EADDRNOTAVAIL = 99, + ENETDOWN = 100, + ENETUNREACH = 101, + ENETRESET = 102, + ECONNABORTED = 103, + ECONNRESET = 104, + ENOBUFS = 105, + EISCONN = 106, + ENOTCONN = 107, + ESHUTDOWN = 108, + ETOOMANYREFS = 109, + ETIMEDOUT = 110, + ECONNREFUSED = 111, + EHOSTDOWN = 112, + EHOSTUNREACH = 113, + EALREADY = 114, + EINPROGRESS = 115, + ESTALE = 116, + EUCLEAN = 117, + ENOTNAM = 118, + ENAVAIL = 119, + EISNAM = 120, + EREMOTEIO = 121, + EDQUOT = 122, + ENOMEDIUM = 123, + EMEDIUMTYPE = 124, + ECANCELED = 125, + ENOKEY = 126, + EKEYEXPIRED = 127, + EKEYREVOKED = 128, + EKEYREJECTED = 129, + EOWNERDEAD = 130, + ENOTRECOVERABLE = 131, + ERFKILL = 132, + EHWPOISON = 133, + EAGAIN = 134, +} + +#[stable(feature = "mikros", since = "1.80.0")] +impl TryFrom for Errno { + type Error = i32; + + fn try_from(value: i32) -> Result { + use Errno::*; + match value { + 1 => Ok(EPERM), + 2 => Ok(ENOENT), + 3 => Ok(ESRCH), + 4 => Ok(EINTR), + 5 => Ok(EIO), + 6 => Ok(ENXIO), + 7 => Ok(E2BIG), + 8 => Ok(ENOEXEC), + 9 => Ok(EBADF), + 10 => Ok(ECHILD), + 11 => Ok(EWOULDBLOCK), + 12 => Ok(ENOMEM), + 13 => Ok(EACCES), + 14 => Ok(EFAULT), + 15 => Ok(ENOTBLK), + 16 => Ok(EBUSY), + 17 => Ok(EEXIST), + 18 => Ok(EXDEV), + 19 => Ok(ENODEV), + 20 => Ok(ENOTDIR), + 21 => Ok(EISDIR), + 22 => Ok(EINVAL), + 23 => Ok(ENFILE), + 24 => Ok(EMFILE), + 25 => Ok(ENOTTY), + 26 => Ok(ETXTBSY), + 27 => Ok(EFBIG), + 28 => Ok(ENOSPC), + 29 => Ok(ESPIPE), + 30 => Ok(EROFS), + 31 => Ok(EMLINK), + 32 => Ok(EPIPE), + 33 => Ok(EDOM), + 34 => Ok(ERANGE), + 35 => Ok(EDEADLK), + 36 => Ok(ENAMETOOLONG), + 37 => Ok(ENOLCK), + 38 => Ok(ENOSYS), + 39 => Ok(ENOTEMPTY), + 40 => Ok(ELOOP), + 42 => Ok(ENOMSG), + 43 => Ok(EIDRM), + 44 => Ok(ECHRNG), + 45 => Ok(EL2NSYNC), + 46 => Ok(EL3HLT), + 47 => Ok(EL3RST), + 48 => Ok(ELNRNG), + 49 => Ok(EUNATCH), + 50 => Ok(ENOCSI), + 51 => Ok(EL2HLT), + 52 => Ok(EBADE), + 53 => Ok(EBADR), + 54 => Ok(EXFULL), + 55 => Ok(ENOANO), + 56 => Ok(EBADRQC), + 57 => Ok(EBADSLT), + 59 => Ok(EBFONT), + 60 => Ok(ENOSTR), + 61 => Ok(ENODATA), + 62 => Ok(ETIME), + 63 => Ok(ENOSR), + 64 => Ok(ENONET), + 65 => Ok(ENOPKG), + 66 => Ok(EREMOTE), + 67 => Ok(ENOLINK), + 68 => Ok(EADV), + 69 => Ok(ESRMNT), + 70 => Ok(ECOMM), + 71 => Ok(EPROTO), + 72 => Ok(EMULTIHOP), + 73 => Ok(EDOTDOT), + 74 => Ok(EBADMSG), + 75 => Ok(EOVERFLOW), + 76 => Ok(ENOTUNIQ), + 77 => Ok(EBADFD), + 78 => Ok(EREMCHG), + 79 => Ok(ELIBACC), + 80 => Ok(ELIBBAD), + 81 => Ok(ELIBSCN), + 82 => Ok(ELIBMAX), + 83 => Ok(ELIBEXEC), + 84 => Ok(EILSEQ), + 85 => Ok(ERESTART), + 86 => Ok(ESTRPIPE), + 87 => Ok(EUSERS), + 88 => Ok(ENOTSOCK), + 89 => Ok(EDESTADDRREQ), + 90 => Ok(EMSGSIZE), + 91 => Ok(EPROTOTYPE), + 92 => Ok(ENOPROTOOPT), + 93 => Ok(EPROTONOSUPPORT), + 94 => Ok(ESOCKTNOSUPPORT), + 95 => Ok(ENOTSUP), + 96 => Ok(EPFNOSUPPORT), + 97 => Ok(EAFNOSUPPORT), + 98 => Ok(EADDRINUSE), + 99 => Ok(EADDRNOTAVAIL), + 100 => Ok(ENETDOWN), + 101 => Ok(ENETUNREACH), + 102 => Ok(ENETRESET), + 103 => Ok(ECONNABORTED), + 104 => Ok(ECONNRESET), + 105 => Ok(ENOBUFS), + 106 => Ok(EISCONN), + 107 => Ok(ENOTCONN), + 108 => Ok(ESHUTDOWN), + 109 => Ok(ETOOMANYREFS), + 110 => Ok(ETIMEDOUT), + 111 => Ok(ECONNREFUSED), + 112 => Ok(EHOSTDOWN), + 113 => Ok(EHOSTUNREACH), + 114 => Ok(EALREADY), + 115 => Ok(EINPROGRESS), + 116 => Ok(ESTALE), + 117 => Ok(EUCLEAN), + 118 => Ok(ENOTNAM), + 119 => Ok(ENAVAIL), + 120 => Ok(EISNAM), + 121 => Ok(EREMOTEIO), + 122 => Ok(EDQUOT), + 123 => Ok(ENOMEDIUM), + 124 => Ok(EMEDIUMTYPE), + 125 => Ok(ECANCELED), + 126 => Ok(ENOKEY), + 127 => Ok(EKEYEXPIRED), + 128 => Ok(EKEYREVOKED), + 129 => Ok(EKEYREJECTED), + 130 => Ok(EOWNERDEAD), + 131 => Ok(ENOTRECOVERABLE), + 132 => Ok(ERFKILL), + 133 => Ok(EHWPOISON), + 134 => Ok(EAGAIN), + x => Err(x), + } + } +} + +#[stable(feature = "mikros", since = "1.80.0")] +impl crate::fmt::Display for Errno { + fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> Result<(), crate::fmt::Error> { + use Errno::*; + let errno_name = match self { + EPERM => "Operation not permitted", + ENOENT => "No such file or directory", + ESRCH => "No such process", + EINTR => "Interrupted system call", + EIO => "Input/output error", + ENXIO => "No such device or address", + E2BIG => "Argument list too long", + ENOEXEC => "Exec format error", + EBADF => "Bad file descriptor", + ECHILD => "No child processes", + EAGAIN => "Resource temporarily unavailable", + EWOULDBLOCK => "Resource temporarily unavailable", + ENOMEM => "Cannot allocate memory", + EACCES => "Permission denied", + EFAULT => "Bad address", + ENOTBLK => "Block device required", + EBUSY => "Device or resource busy", + EEXIST => "File exists", + EXDEV => "Invalid cross-device link", + ENODEV => "No such device", + ENOTDIR => "Not a directory", + EISDIR => "Is a directory", + EINVAL => "Invalid argument", + ENFILE => "Too many open files in system", + EMFILE => "Too many open files", + ENOTTY => "Inappropriate ioctl for device", + ETXTBSY => "Text file busy", + EFBIG => "File too large", + ENOSPC => "No space left on device", + ESPIPE => "Illegal seek", + EROFS => "Read-only file system", + EMLINK => "Too many links", + EPIPE => "Broken pipe", + EDOM => "Numerical argument out of domain", + ERANGE => "Numerical result out of range", + EDEADLK => "Resource deadlock avoided", + ENAMETOOLONG => "File name too long", + ENOLCK => "No locks available", + ENOSYS => "Function not implemented", + ENOTEMPTY => "Directory not empty", + ELOOP => "Too many levels of symbolic links", + ENOMSG => "No message of desired type", + EIDRM => "Identifier removed", + ECHRNG => "Channel number out of range", + EL2NSYNC => "Level 2 not synchronized", + EL3HLT => "Level 3 halted", + EL3RST => "Level 3 reset", + ELNRNG => "Link number out of range", + EUNATCH => "Protocol driver not attached", + ENOCSI => "No CSI structure available", + EL2HLT => "Level 2 halted", + EBADE => "Invalid exchange", + EBADR => "Invalid request descriptor", + EXFULL => "Exchange full", + ENOANO => "No anode", + EBADRQC => "Invalid request code", + EBADSLT => "Invalid slot", + EBFONT => "Bad font file format", + ENOSTR => "Device not a stream", + ENODATA => "No data available", + ETIME => "Timer expired", + ENOSR => "Out of streams resources", + ENONET => "Machine is not on the network", + ENOPKG => "Package not installed", + EREMOTE => "Object is remote", + ENOLINK => "Link has been severed", + EADV => "Advertise error", + ESRMNT => "Srmount error", + ECOMM => "Communication error on send", + EPROTO => "Protocol error", + EMULTIHOP => "Multihop attempted", + EDOTDOT => "RFS specific error", + EBADMSG => "Bad message", + EOVERFLOW => "Value too large for defined data type", + ENOTUNIQ => "Name not unique on network", + EBADFD => "File descriptor in bad state", + EREMCHG => "Remote address changed", + ELIBACC => "Can not access a needed shared library", + ELIBBAD => "Accessing a corrupted shared library", + ELIBSCN => ".lib section in a.out corrupted", + ELIBMAX => "Attempting to link in too many shared libraries", + ELIBEXEC => "Cannot exec a shared library directly", + EILSEQ => "Invalid or incomplete multibyte or wide character", + ERESTART => "Interrupted system call should be restarted", + ESTRPIPE => "Streams pipe error", + EUSERS => "Too many users", + ENOTSOCK => "Socket operation on non-socket", + EDESTADDRREQ => "Destination address required", + EMSGSIZE => "Message too long", + EPROTOTYPE => "Protocol wrong type for socket", + ENOPROTOOPT => "Protocol not available", + EPROTONOSUPPORT => "Protocol not supported", + ESOCKTNOSUPPORT => "Socket type not supported", + ENOTSUP => "Operation not supported", + EPFNOSUPPORT => "Protocol family not supported", + EAFNOSUPPORT => "Address family not supported by protocol", + EADDRINUSE => "Address already in use", + EADDRNOTAVAIL => "Cannot assign requested address", + ENETDOWN => "Network is down", + ENETUNREACH => "Network is unreachable", + ENETRESET => "Network dropped connection on reset", + ECONNABORTED => "Software caused connection abort", + ECONNRESET => "Connection reset by peer", + ENOBUFS => "No buffer space available", + EISCONN => "Transport endpoint is already connected", + ENOTCONN => "Transport endpoint is not connected", + ESHUTDOWN => "Cannot send after transport endpoint shutdown", + ETOOMANYREFS => "Too many references: cannot splice", + ETIMEDOUT => "Connection timed out", + ECONNREFUSED => "Connection refused", + EHOSTDOWN => "Host is down", + EHOSTUNREACH => "No route to host", + EALREADY => "Operation already in progress", + EINPROGRESS => "Operation now in progress", + ESTALE => "Stale file handle", + EUCLEAN => "Structure needs cleaning", + ENOTNAM => "Not a XENIX named type file", + ENAVAIL => "No XENIX semaphores available", + EISNAM => "Is a named type file", + EREMOTEIO => "Remote I/O error", + EDQUOT => "Disk quota exceeded", + ENOMEDIUM => "No medium found", + EMEDIUMTYPE => "Wrong medium type", + ECANCELED => "Operation canceled", + ENOKEY => "Required key not available", + EKEYEXPIRED => "Key has expired", + EKEYREVOKED => "Key has been revoked", + EKEYREJECTED => "Key was rejected by service", + EOWNERDEAD => "Owner died", + ENOTRECOVERABLE => "State not recoverable", + ERFKILL => "Operation not possible due to RF-kill", + EHWPOISON => "Memory page has hardware error", + }; + f.write_str(errno_name) + } +} diff --git a/library/std/src/os/mikros/mod.rs b/library/std/src/os/mikros/mod.rs index a827e63fef9..ddbd21c2b23 100644 --- a/library/std/src/os/mikros/mod.rs +++ b/library/std/src/os/mikros/mod.rs @@ -9,6 +9,11 @@ pub mod loader; pub mod syscalls; +mod errno; + +#[stable(feature = "mikros", since = "1.80.0")] +pub use errno::Errno; + /// A prelude for conveniently writing platform-specific code. /// /// Includes all extension traits, and some important type definitions. diff --git a/library/std/src/sys/pal/mikros/common.rs b/library/std/src/sys/pal/mikros/common.rs index ac9324f5d98..69897bf02b2 100644 --- a/library/std/src/sys/pal/mikros/common.rs +++ b/library/std/src/sys/pal/mikros/common.rs @@ -1,4 +1,5 @@ use crate::io as std_io; +use crate::os::mikros::Errno; use crate::os::mikros::ipc::rpc; // SAFETY: must be called only once during runtime initialization. @@ -21,12 +22,54 @@ pub fn unsupported_err() -> std_io::Error { std_io::Error::UNSUPPORTED_PLATFORM } -pub fn is_interrupted(_code: i32) -> bool { - false +pub fn is_interrupted(errno: RawOsError) -> bool { + errno == Errno::EINTR } -pub fn decode_error_kind(_code: i32) -> crate::io::ErrorKind { - crate::io::ErrorKind::Uncategorized +pub type RawOsError = Errno; + +pub fn decode_error_kind(errno: RawOsError) -> crate::io::ErrorKind { + use crate::io::ErrorKind::*; + + match errno { + Errno::E2BIG => ArgumentListTooLong, + Errno::EADDRINUSE => AddrInUse, + Errno::EADDRNOTAVAIL => AddrNotAvailable, + Errno::EBUSY => ResourceBusy, + Errno::ECONNABORTED => ConnectionAborted, + Errno::ECONNREFUSED => ConnectionRefused, + Errno::ECONNRESET => ConnectionReset, + Errno::EDEADLK => Deadlock, + Errno::EDQUOT => FilesystemQuotaExceeded, + Errno::EEXIST => AlreadyExists, + Errno::EFBIG => FileTooLarge, + Errno::EHOSTUNREACH => HostUnreachable, + Errno::EINTR => Interrupted, + Errno::EINVAL => InvalidInput, + Errno::EISDIR => IsADirectory, + Errno::ELOOP => FilesystemLoop, + Errno::ENOENT => NotFound, + Errno::ENOMEM => OutOfMemory, + Errno::ENOSPC => StorageFull, + Errno::ENOSYS => Unsupported, + Errno::EMLINK => TooManyLinks, + Errno::ENAMETOOLONG => InvalidFilename, + Errno::ENETDOWN => NetworkDown, + Errno::ENETUNREACH => NetworkUnreachable, + Errno::ENOTCONN => NotConnected, + Errno::ENOTDIR => NotADirectory, + Errno::ENOTEMPTY => DirectoryNotEmpty, + Errno::EPIPE => BrokenPipe, + Errno::EROFS => ReadOnlyFilesystem, + Errno::ESPIPE => NotSeekable, + Errno::ESTALE => StaleNetworkFileHandle, + Errno::ETIMEDOUT => TimedOut, + Errno::ETXTBSY => ExecutableFileBusy, + Errno::EXDEV => CrossesDevices, + Errno::EACCES | Errno::EPERM => PermissionDenied, + Errno::EAGAIN | Errno::EWOULDBLOCK => WouldBlock, + _ => Uncategorized, + } } pub fn abort_internal() -> ! { diff --git a/library/std/src/sys/pal/mikros/os.rs b/library/std/src/sys/pal/mikros/os.rs index f36fd39ddf2..25f48acda2c 100644 --- a/library/std/src/sys/pal/mikros/os.rs +++ b/library/std/src/sys/pal/mikros/os.rs @@ -3,15 +3,16 @@ use crate::error::Error as StdError; use crate::ffi::{OsStr, OsString}; use crate::marker::PhantomData; +use crate::os::mikros::Errno; use crate::path::{self, PathBuf}; use crate::{fmt, io}; -pub fn errno() -> i32 { - 0 +pub fn errno() -> Errno { + Errno::EIO } -pub fn error_string(_errno: i32) -> String { - "operation successful".to_string() +pub fn error_string(errno: Errno) -> String { + errno.to_string() } pub fn getcwd() -> io::Result { diff --git a/library/std/src/sys/pal/mod.rs b/library/std/src/sys/pal/mod.rs index f40920902f5..87258bc0500 100644 --- a/library/std/src/sys/pal/mod.rs +++ b/library/std/src/sys/pal/mod.rs @@ -79,5 +79,5 @@ } } -#[cfg(not(target_os = "uefi"))] +#[cfg(not(any(target_os = "uefi", target_os = "mikros")))] pub type RawOsError = i32;