Skip to main content

dope_core/driver/storage/
mapping.rs

1use std::{convert::Infallible, mem};
2
3use dope_sys::{
4    memory,
5    ring::{layout, map},
6};
7
8use crate::driver::{
9    result::{self, IoOperation, Kernel},
10    storage::{Address, Bytes, Count},
11};
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum Mapping {
15    Unregister {
16        cause: crate::Errno,
17    },
18    Length {
19        element: Bytes,
20        count: Count,
21    },
22    Alignment {
23        element: Bytes,
24        supported: Bytes,
25    },
26    Syscall {
27        operation: IoOperation,
28        bytes: Bytes,
29        cause: result::Kernel,
30    },
31    Ring {
32        operation: IoOperation,
33        region: layout::Region,
34        bytes: Bytes,
35        cause: result::Kernel,
36    },
37    Unmap {
38        operation: IoOperation,
39        address: Address,
40        bytes: Bytes,
41        cause: result::Kernel,
42    },
43    Null {
44        operation: IoOperation,
45        bytes: Bytes,
46    },
47    RingNull {
48        operation: IoOperation,
49        region: layout::Region,
50        bytes: Bytes,
51    },
52}
53
54impl Mapping {
55    pub(crate) const fn unmap(
56        op: IoOperation,
57        address: Address,
58        bytes: Bytes,
59        cause: result::Kernel,
60    ) -> Self {
61        Self::Unmap {
62            operation: op,
63            address,
64            bytes,
65            cause,
66        }
67    }
68}
69
70const _: () = {
71    assert!(mem::size_of::<Address>() == mem::size_of::<*mut core::ffi::c_void>());
72    assert!(mem::size_of::<Bytes>() == mem::size_of::<usize>());
73};
74
75impl<R> From<(R, Infallible)> for Mapping {
76    fn from((_, empty): (R, Infallible)) -> Self {
77        match empty {}
78    }
79}
80
81impl From<memory::Error> for Mapping {
82    fn from(error: memory::Error) -> Self {
83        match error {
84            memory::Error::Length { element, count } => Self::Length {
85                element: Bytes::new(element),
86                count: Count::new(count),
87            },
88            memory::Error::Alignment {
89                element,
90                supported: limit,
91            } => Self::Alignment {
92                element: Bytes::new(element),
93                supported: Bytes::new(limit),
94            },
95            memory::Error::Map { bytes, cause } => Self::Syscall {
96                operation: IoOperation::MapBuffers,
97                bytes: Bytes::new(bytes.get()),
98                cause: Kernel::from(cause),
99            },
100            memory::Error::Populate { bytes, cause } => Self::Syscall {
101                operation: IoOperation::PopulateBuffers,
102                bytes: Bytes::new(bytes.get()),
103                cause: Kernel::from(cause),
104            },
105            memory::Error::Null { bytes } => Self::Null {
106                operation: IoOperation::MapBuffers,
107                bytes: Bytes::new(bytes.get()),
108            },
109            memory::Error::Unmap {
110                address,
111                bytes,
112                cause,
113            } => Self::Unmap {
114                operation: IoOperation::UnmapBuffers,
115                address: Address::new(address),
116                bytes: Bytes::new(bytes.get()),
117                cause: Kernel::from(cause),
118            },
119        }
120    }
121}
122
123impl<E> From<map::Storage<E>> for Mapping
124where
125    Kernel: From<E>,
126{
127    fn from(error: map::Storage<E>) -> Self {
128        match error {
129            map::Storage::Ring(failure) => Self::from(failure),
130            map::Storage::Buffer(failure) => Self::from(failure),
131            map::Storage::Unregister(cause) => Self::Unregister {
132                cause: cause.into(),
133            },
134        }
135    }
136}
137
138impl<E> From<map::Map<E>> for Mapping
139where
140    Kernel: From<E>,
141{
142    fn from(error: map::Map<E>) -> Self {
143        match error {
144            map::Map::Syscall { region, len, code } => Self::Ring {
145                operation: IoOperation::MapRing,
146                region,
147                bytes: Bytes::new(len.get()),
148                cause: Kernel::from(code),
149            },
150            map::Map::Null { region, len } => Self::RingNull {
151                operation: IoOperation::MapRing,
152                region,
153                bytes: Bytes::new(len.get()),
154            },
155        }
156    }
157}
158
159impl<E> From<map::Failure<E>> for Mapping
160where
161    Kernel: From<E>,
162{
163    fn from(failure: map::Failure<E>) -> Self {
164        let (address, bytes, code) = failure.into_parts();
165        Self::unmap(
166            IoOperation::UnmapRing,
167            Address::new(address),
168            Bytes::new(bytes.get()),
169            Kernel::from(code),
170        )
171    }
172}