1pub mod poll;
2
3use std::{convert, mem};
4
5use dope_sys::{
6 io::reactor::{Full, complete},
7 kernel,
8 memory::Cleanup,
9 probe,
10 ring::prepare::Feature,
11};
12use o3::{alloc, buffer::pool, depot::arena::slab::Vacancy};
13
14use crate::{
15 Engine,
16 driver::{
17 life::domain,
18 receive,
19 result::poll::fault,
20 route::{indices, table::bounds},
21 settings::errors,
22 storage,
23 },
24 executor,
25 io::{socket, transfer},
26};
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub enum IoOperation {
30 Entropy,
31 ReactorCreate,
32 MapRing,
33 RegisterReactor,
34 CancelRegistrations,
35 RegisterOpcodes,
36 RegisterFileTable,
37 RegisterFileRange,
38 MapBuffers,
39 UnmapRing,
40 UnmapBuffers,
41 PopulateBuffers,
42 RegisterProvidedBuffers,
43 RegisterCanaryBuffers,
44 UnregisterCanaryBuffers,
45 InstallReceiveFile,
46 RemoveReceiveFile,
47 InstallReceiveMessageFile,
48 RemoveReceiveMessageFile,
49 Probe(Probe),
50}
51
52#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub enum Cloexec {
54 Get(crate::Errno),
55 Set(crate::Errno),
56}
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
59pub enum Capability {
60 Feature(Feature),
61 Opcode(Opcode),
62}
63
64#[derive(Clone, Copy, Debug, PartialEq, Eq)]
65#[repr(transparent)]
66pub struct Opcode(<crate::Backend as kernel::Source>::Code);
67
68impl<C> Opcode
69where
70 crate::Backend: kernel::Source<Code = C>,
71{
72 pub(crate) fn native<T: Into<C>>(code: T) -> Self {
73 Self(code.into())
74 }
75}
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78#[repr(u8)]
79pub enum Probe {
80 Ring = 0,
81 DirectSocket = 1,
82 SocketOption = 2,
83 CloseDirectSocket = 3,
84 Receive = 4,
85 ReceiveMessage = 5,
86 DirectAccept = 6,
87 CloseDirectAccept = 7,
88 DirectAcceptMultishot = 8,
89 CloseDirectAcceptMultishot = 9,
90 RegisterAcceptListener = 10,
91 ReleaseAcceptListener = 11,
92}
93
94impl From<Probe> for u64 {
95 fn from(probe: Probe) -> Self {
96 probe as Self
97 }
98}
99
100#[derive(Clone, Copy, Debug, PartialEq, Eq)]
101pub struct ProbeError {
102 probe: Probe,
103 violation: Violation,
104}
105
106#[derive(Clone, Copy, Debug, PartialEq, Eq)]
107pub enum Violation {
108 MissingCompletion,
109 UnexpectedCompletion {
110 expected: Probe,
111 observed: fault::Observation,
112 },
113 Rejected(fault::Observation),
114 UnexpectedResult(fault::Observation),
115 Send(fault::Send),
116 InvalidFlags(fault::Observation),
117 MissingBuffer,
118 InvalidBuffer(receive::Selection),
119 BufferLength {
120 actual: transfer::Len,
121 capacity: transfer::NonZero,
122 },
123 EarlyTermination,
124 CorruptPayload,
125 FrameOverflow {
126 name: receive::Capacity,
127 control: receive::Capacity,
128 },
129 FrameShort {
130 actual: receive::Length,
131 required: receive::Length,
132 },
133 MissingFrameTruncation {
134 region: FrameRegion,
135 reported: receive::Length,
136 available: receive::Length,
137 },
138 TruncatedFrame,
139 InvalidAddress(socket::Error),
140 NonterminalCancellation(fault::Observation),
141 EscapedSlot(bounds::Slot),
142 ResidualWork {
143 submissions: fault::Pending,
144 completions: fault::Available,
145 },
146}
147
148#[derive(Clone, Copy, PartialEq, Eq, Debug)]
149pub enum FrameRegion {
150 Control,
151 Payload,
152}
153
154impl ProbeError {
155 pub(crate) fn decode(
156 failure: probe::Failure<Probe, <Engine as complete::Observe>::Observation>,
157 ) -> Self {
158 use dope_sys::probe::Check;
159 let (probe, check) = failure.into_parts();
160 let fault = match check {
161 Check::InvalidFlags(observed) => Violation::InvalidFlags((&observed).into()),
162 Check::Rejected(observed) => Violation::Rejected((&observed).into()),
163 Check::Unexpected(observed) => Violation::UnexpectedResult((&observed).into()),
164 Check::Nonterminal(observed) => Violation::NonterminalCancellation((&observed).into()),
165 };
166 Self::new(probe, fault)
167 }
168
169 pub(crate) const fn new(probe: Probe, fault: Violation) -> Self {
170 Self {
171 probe,
172 violation: fault,
173 }
174 }
175
176 pub const fn into_parts(self) -> (Probe, Violation) {
177 (self.probe, self.violation)
178 }
179}
180
181#[derive(Clone, Copy, Debug, Eq, PartialEq)]
182#[repr(transparent)]
183pub struct SubmitError(SubmitCause);
184
185#[derive(Clone, Copy, Debug, Eq, PartialEq)]
186pub enum SubmitCause {
187 Full,
189 IdentityExhausted,
191}
192
193impl SubmitError {
194 pub fn cause(self) -> SubmitCause {
195 self.0
196 }
197}
198
199impl From<Full> for SubmitError {
200 fn from(_: Full) -> Self {
201 Self(SubmitCause::Full)
202 }
203}
204
205impl From<Vacancy> for SubmitError {
206 fn from(error: Vacancy) -> Self {
207 Self(match error {
208 Vacancy::Full => SubmitCause::Full,
209 Vacancy::IdentityExhausted => SubmitCause::IdentityExhausted,
210 })
211 }
212}
213
214#[derive(Clone, Copy, Debug, PartialEq, Eq)]
215#[repr(transparent)]
216pub struct Kernel(kernel::Diagnostic<indices::SlotIndex>);
217
218impl Kernel {
219 pub fn cause(self) -> kernel::Diagnostic<indices::SlotIndex> {
221 self.0
222 }
223
224 pub(crate) fn native<T: Into<kernel::Diagnostic<indices::SlotIndex>>>(cause: T) -> Self {
225 Self(cause.into())
226 }
227}
228
229impl<O> From<kernel::Errno<O>> for Kernel
230where
231 <crate::Backend as kernel::Source>::Failure<indices::SlotIndex>: From<kernel::Errno<O>>,
232{
233 fn from(cause: kernel::Errno<O>) -> Self {
234 let cause: <crate::Backend as kernel::Source>::Failure<indices::SlotIndex> = cause.into();
235 Self::native(cause)
236 }
237}
238
239impl From<convert::Infallible> for Kernel {
240 fn from(value: convert::Infallible) -> Self {
241 match value {}
242 }
243}
244
245impl From<dope_sys::Errno> for Kernel {
246 fn from(cause: dope_sys::Errno) -> Self {
247 Self(cause.into())
248 }
249}
250
251const _: () = {
252 assert!(
253 mem::size_of::<Kernel>()
254 == mem::size_of::<<crate::Backend as kernel::Source>::Failure<indices::SlotIndex>>()
255 );
256 assert!(
257 mem::align_of::<Kernel>()
258 == mem::align_of::<<crate::Backend as kernel::Source>::Failure<indices::SlotIndex>>()
259 );
260};
261
262type ReleasePrimary = ((IoOperation, Kernel), crate::RemoveSlot);
263
264#[derive(Debug)]
266pub enum Refused {
267 Plan(errors::Config),
268 Allocation(alloc::Error),
269 Queue(alloc::Error),
270 Buffer(pool::CreateError),
271 ReactorCloexec(Cloexec),
272 Native(storage::Failure),
273 Removal(storage::Removal),
274 Register(domain::Register),
275}
276
277impl Refused {
278 pub(crate) fn kernel(operation: IoOperation, cause: Kernel) -> Self {
279 Self::Native(storage::Failure::Kernel { operation, cause })
280 }
281}
282
283impl<E> executor::Error<'_>
284where
285 crate::Resources: Cleanup<storage::Failure, Error = E>,
286 storage::Failure: From<E>,
287{
288 pub fn kind(&self) -> executor::Kind<'_> {
289 match self {
290 Self::Refused(error) => executor::Kind::Refused(error),
291 Self::Cleanup(state) => executor::Kind::Cleanup {
292 primary: state.primary(),
293 cleanup: storage::Failure::from(Cleanup::<storage::Failure>::failure(&**state)),
294 },
295 Self::Release(state) => release_kind::<ReleasePrimary, _>(&**state),
296 }
297 }
298}
299
300fn release_kind<P, R: Cleanup<P>>(state: &R) -> executor::Kind<'_>
301where
302 storage::Removal: From<P>,
303 storage::Mapping: From<R::Error>,
304{
305 executor::Kind::Release {
306 primary: state.primary().into(),
307 cleanup: state.failure().into(),
308 }
309}
310
311impl From<alloc::Error> for Refused {
312 fn from(cause: alloc::Error) -> Self {
313 Self::Allocation(cause)
314 }
315}
316
317impl From<alloc::Overflow> for Refused {
318 fn from(cause: alloc::Overflow) -> Self {
319 Self::Allocation(cause.into())
320 }
321}
322
323impl From<alloc::Exhausted> for Refused {
324 fn from(cause: alloc::Exhausted) -> Self {
325 Self::Allocation(cause.into())
326 }
327}