Skip to main content

dope_core/driver/life/native/create/
mapping.rs

1use std::convert::Infallible;
2
3use dope_sys::{memory, ring::map};
4
5pub(crate) use crate::driver::storage::Mapping;
6use crate::driver::{
7    life::native::create::Kernel,
8    result::IoOperation,
9    storage::{Address, Bytes, Count},
10};
11impl<R> From<(R, Infallible)> for Mapping {
12    fn from((_, empty): (R, Infallible)) -> Self {
13        match empty {}
14    }
15}
16
17impl From<memory::Error> for Mapping {
18    fn from(error: memory::Error) -> Self {
19        match error {
20            memory::Error::Length { element, count } => Self::Length {
21                element: Bytes::new(element),
22                count: Count::new(count),
23            },
24            memory::Error::Alignment {
25                element,
26                supported: limit,
27            } => Self::Alignment {
28                element: Bytes::new(element),
29                supported: Bytes::new(limit),
30            },
31            memory::Error::Map { bytes, cause } => Self::Syscall {
32                operation: IoOperation::MapBuffers,
33                bytes: Bytes::new(bytes.get()),
34                cause: Kernel::from(cause),
35            },
36            memory::Error::Populate { bytes, cause } => Self::Syscall {
37                operation: IoOperation::PopulateBuffers,
38                bytes: Bytes::new(bytes.get()),
39                cause: Kernel::from(cause),
40            },
41            memory::Error::Null { bytes } => Self::Null {
42                operation: IoOperation::MapBuffers,
43                bytes: Bytes::new(bytes.get()),
44            },
45            memory::Error::Unmap {
46                address,
47                bytes,
48                cause,
49            } => Self::Unmap {
50                operation: IoOperation::UnmapBuffers,
51                address: Address::new(address),
52                bytes: Bytes::new(bytes.get()),
53                cause: Kernel::from(cause),
54            },
55        }
56    }
57}
58
59impl<E> From<map::Storage<E>> for Mapping
60where
61    Kernel: From<E>,
62{
63    fn from(error: map::Storage<E>) -> Self {
64        match error {
65            map::Storage::Ring(failure) => Self::from(failure),
66            map::Storage::Buffer(failure) => Self::from(failure),
67            map::Storage::Unregister(cause) => Self::Unregister {
68                cause: cause.into(),
69            },
70        }
71    }
72}
73
74impl<E> From<map::Map<E>> for Mapping
75where
76    Kernel: From<E>,
77{
78    fn from(error: map::Map<E>) -> Self {
79        match error {
80            map::Map::Syscall { region, len, code } => Self::Ring {
81                operation: IoOperation::MapRing,
82                region,
83                bytes: Bytes::new(len.get()),
84                cause: Kernel::from(code),
85            },
86            map::Map::Null { region, len } => Self::RingNull {
87                operation: IoOperation::MapRing,
88                region,
89                bytes: Bytes::new(len.get()),
90            },
91        }
92    }
93}
94
95impl<E> From<map::Failure<E>> for Mapping
96where
97    Kernel: From<E>,
98{
99    fn from(failure: map::Failure<E>) -> Self {
100        let (address, bytes, code) = failure.into_parts();
101        Self::unmap(
102            IoOperation::UnmapRing,
103            Address::new(address),
104            Bytes::new(bytes.get()),
105            Kernel::from(code),
106        )
107    }
108}