Skip to main content

kernel/syscall/
result.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Syscall Result Conversion and Error Types
3//!
4//! Authors: MarioS271
5
6/// A trait to convert the implementing type to a valid syscall result `u64`
7pub trait ToSyscallU64 {
8    /// Convert the given `self` into a `u64`
9    fn as_syscall_u64(&self) -> u64;
10}
11
12impl ToSyscallU64 for () {
13    /// Always returns `0u64`
14    fn as_syscall_u64(&self) -> u64 {
15        0u64
16    }
17}
18
19
20/// Wrapper type for `Result<T, SyscallError>`
21pub type SyscallResult<T> = Result<T, SyscallError>;
22
23/// A trait to implement methods into the [`SyscallResult`] type
24pub trait SyscallResultTrait {
25    /// Convert a given [`SyscallError`] into a `u32`
26    fn as_u32(&self) -> u32;
27}
28impl<T> SyscallResultTrait for SyscallResult<T> {
29    fn as_u32(&self) -> u32 {
30        match self {
31            Ok(_) => 0,
32            Err(e) => *e as u32
33        }
34    }
35}
36
37#[derive(Copy, Clone)]
38#[repr(u32)]
39pub enum SyscallError {
40    InvalidSyscall = 1,
41    InvalidArgument = 2,
42    InternalError = 3,
43    BadAddress = 4
44}