Skip to main content

kernel_api/syscall/
result.rs

1use core::fmt;
2use crate::allocator::AllocError;
3use crate::ptr::PointerError;
4
5/// A specialized [`Result`](`core::result::Result`) type for syscalls.
6pub type Result<T> = core::result::Result<T, Error>;
7
8macro_rules! define_error {
9    ($(#[$attr:meta])* pub enum Error {
10	    $($(#[$item_attr:meta])* $name:ident = $val:literal),*
11	    $(,)?
12    }) => {
13	    $(#[$attr])* pub enum Error {
14		    _Reserved = 0,
15	        $($(#[$item_attr])* $name = $val),* ,
16		    _MaxVal,
17        }
18
19	    const _: () = { assert!((Error::_MaxVal as u16) < 4096, "cannot have more than 4096 error codes"); };
20
21	    impl From<u32> for Error {
22			fn from(value: u32) -> Error {
23				match value {
24					$($val => Error::$name),*,
25					_ => Self::Invalid,
26				}
27			}
28		}
29    };
30}
31
32define_error! {
33	/// Types of error that can be returned from a syscall.
34	#[derive(Debug, Copy, Clone, Eq, PartialEq)]
35	#[repr(u16)]
36	#[non_exhaustive]
37	pub enum Error {
38		/// String argument passed was malformed UTF-8.
39		InvalidUtf8 = 1,
40		/// Server or handle does not support requested protocol or method.
41		UnsupportedProtocol = 2,
42		/// Unknown protocol or method.
43		UnknownProtocol = 3,
44		/// Requested endpoint name is invalid or does not exist.
45		InvalidEndpoint = 4,
46		/// Requested endpoint name is already in use.
47		NameInUse = 5,
48		/// Handle argument passed was invalid.
49		InvalidHandle = 6,
50		/// Too many open objects.
51		Overflow = 7,
52		/// Pointer argument passed was invalid.
53		InvalidPointer = 8,
54		/// Server is no longer running.
55		DeadServer = 9,
56		/// Incorrect return type for called method.
57		InvalidReturn = 10,
58		/// Generic error.
59		Invalid = 11,
60		/// Failed to allocate memory.
61		AllocationFailure = 12,
62		/// Argument passed is invalid.
63		InvalidArg = 13,
64		/// Requested method does not support being called asynchronously.
65		AsyncUnsupported = 14,
66		/// Passed argument value is reserved for future use.
67		FutureCompat = 15,
68		/// Object is not long enough for passed arguments.
69		EndOfData = 16,
70		/// Protocol already exists on handle.
71		ProtocolOverlap = 17,
72		/// Condition required for method not met.
73		ConditionNotMet = 18,
74	}
75}
76
77impl From<PointerError> for Error {
78	fn from(_: PointerError) -> Self {
79		Self::InvalidPointer
80	}
81}
82
83impl From<AllocError> for Error {
84	fn from(_: AllocError) -> Self {
85		Self::AllocationFailure
86	}
87}
88
89impl fmt::Display for Error {
90	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91		write!(f, "syscall failed")
92	}
93}
94
95impl core::error::Error for Error {}