1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use core::convert::TryFrom;

pub mod envelope;
pub use envelope::Envelope as MessageEnvelope;

pub mod sender;
pub use sender::Sender as MessageSender;

pub mod id;
pub use id::Id as MessageId;

use super::{MemoryAddress, MemoryRange, MemorySize};

#[repr(C)]
#[derive(Debug, PartialEq)]
/// A struct describing memory that is passed between processes.
/// The `buf` value will get translated as necessary.
pub struct MemoryMessage {
    /// A user-assignable message ID.
    pub id: MessageId,

    /// The offset of the buffer.  This address will get transformed when the
    /// message is moved between processes.
    pub buf: MemoryRange,

    /// The offset within the buffer where the interesting stuff starts. The usage
    /// of this field is purely advisory, is not checked by the kernel, should not
    /// be trusted by the receiver. It is perfectly legal for this to be larger
    /// than the buffer size.
    ///
    /// As a result, this field may be repurposed for other uses. For example,
    /// you can store a `usize` in this field by setting
    /// `message.offset = MemoryAddress::new(val)`, and get a `usize` back by
    /// reading `message.offset.map(|v| v.get()).unwrap_or_default()`.
    ///
    /// For `MutableBorrow` messages this value will be returned to the sender and the
    /// field will be updated when the Message is returned. Therefore you may also use
    /// this field to communicate additional information to the message sender.
    pub offset: Option<MemoryAddress>,

    /// How many bytes in the buffer are valid. This field is advisory, and is not
    /// checked by the kernel. It is legal for the sender to specify a `valid` range
    /// that is larger than `buf.len()`, so this value should not be blindly trusted.
    ///
    /// As a result, this field may be repurposed for other uses. For example,
    /// you can store a `usize` in this field by setting
    /// `message.valid = MemoryAddress::new(val)`, and get a `usize` back by
    /// reading `message.valid.map(|v| v.get()).unwrap_or_default()`.
    ///
    /// For `MutableBorrow` messages this value will be returned to the sender and the
    /// field will be updated when the Message is returned. Therefore you may also use
    /// this field to communicate additional information to the message sender.
    pub valid: Option<MemorySize>,
}

impl MemoryMessage {
    pub fn from_usize(
        id: usize,
        addr: usize,
        size: usize,
        offset: usize,
        valid: usize,
    ) -> Option<MemoryMessage> {
        let addr = match MemoryAddress::new(addr) {
            None => return None,
            Some(s) => s,
        };
        let size = match MemorySize::new(size) {
            None => return None,
            Some(s) => s,
        };
        let buf = MemoryRange { addr, size };
        let offset = MemoryAddress::new(offset);
        let valid = MemorySize::new(valid);

        Some(MemoryMessage {
            id,
            buf,
            offset,
            valid,
        })
    }
    pub fn to_usize(&self) -> [usize; 5] {
        [
            self.id,
            self.buf.addr.get(),
            self.buf.size.get(),
            self.offset.map(|e| e.get()).unwrap_or(0),
            self.valid.map(|e| e.get()).unwrap_or(0),
        ]
    }
}

#[repr(C)]
#[derive(Debug, PartialEq, Clone, Copy, Default)]
/// A simple scalar message.  This is similar to a `move` message.
pub struct ScalarMessage {
    pub id: MessageId,
    pub arg1: usize,
    pub arg2: usize,
    pub arg3: usize,
    pub arg4: usize,
}

impl ScalarMessage {
    pub fn from_usize(
        id: usize,
        arg1: usize,
        arg2: usize,
        arg3: usize,
        arg4: usize,
    ) -> ScalarMessage {
        ScalarMessage {
            id,
            arg1,
            arg2,
            arg3,
            arg4,
        }
    }
    pub fn to_usize(&self) -> [usize; 5] {
        [self.id, self.arg1, self.arg2, self.arg3, self.arg4]
    }
}

#[repr(usize)]
#[derive(Debug, PartialEq)]
pub enum Message {
    MutableBorrow(MemoryMessage),
    Borrow(MemoryMessage),
    Move(MemoryMessage),
    Scalar(ScalarMessage),
    BlockingScalar(ScalarMessage),
}

impl Message {
    pub fn new_scalar(
        id: usize,
        arg1: usize,
        arg2: usize,
        arg3: usize,
        arg4: usize,
    ) -> crate::Message {
        Message::Scalar(crate::ScalarMessage {
            id,
            arg1,
            arg2,
            arg3,
            arg4,
        })
    }

    pub fn new_blocking_scalar(
        id: usize,
        arg1: usize,
        arg2: usize,
        arg3: usize,
        arg4: usize,
    ) -> crate::Message {
        Message::BlockingScalar(crate::ScalarMessage {
            id,
            arg1,
            arg2,
            arg3,
            arg4,
        })
    }

    pub fn new_lend(
        id: usize,
        buf: MemoryRange,
        offset: Option<MemoryAddress>,
        valid: Option<MemorySize>,
    ) -> crate::Message {
        Message::Borrow(crate::MemoryMessage {
            id,
            buf,
            offset,
            valid,
        })
    }

    pub fn new_lend_mut(
        id: usize,
        buf: MemoryRange,
        offset: Option<MemoryAddress>,
        valid: Option<MemorySize>,
    ) -> crate::Message {
        Message::MutableBorrow(crate::MemoryMessage {
            id,
            buf,
            offset,
            valid,
        })
    }

    /// Determine whether the specified Message will block
    pub fn is_blocking(&self) -> bool {
        match *self {
            Message::MutableBorrow(_) | Message::Borrow(_) | Message::BlockingScalar(_) => true,
            Message::Move(_) | Message::Scalar(_) => false,
        }
    }

    /// Determine whether the specified message has data attached
    pub fn has_memory(&self) -> bool {
        match *self {
            Message::MutableBorrow(_) | Message::Borrow(_) | Message::Move(_) => true,
            Message::BlockingScalar(_) | Message::Scalar(_) => false,
        }
    }

    pub fn is_scalar(&self) -> bool {
        !self.has_memory()
    }

    pub fn memory(&self) -> Option<&MemoryRange> {
        self.memory_message().map(|msg| &msg.buf)
    }

    pub fn memory_message(&self) -> Option<&MemoryMessage> {
        match self {
            Message::MutableBorrow(mem) | Message::Borrow(mem) | Message::Move(mem) => Some(mem),
            Message::BlockingScalar(_) | Message::Scalar(_) => None,
        }
    }

    pub fn memory_message_mut(&mut self) -> Option<&mut MemoryMessage> {
        match self {
            Message::MutableBorrow(mem) | Message::Move(mem) => Some(mem),
            Message::BlockingScalar(_) | Message::Scalar(_) | Message::Borrow(_) => None,
        }
    }

    pub fn scalar_message(&self) -> Option<&ScalarMessage> {
        match self {
            Message::MutableBorrow(_) | Message::Borrow(_) | Message::Move(_) => None,
            Message::BlockingScalar(scalar) | Message::Scalar(scalar) => Some(scalar),
        }
    }

    pub fn scalar_message_mut(&mut self) -> Option<&mut ScalarMessage> {
        match self {
            Message::MutableBorrow(_)
            | Message::Borrow(_)
            | Message::Move(_)
            | Message::Scalar(_) => None,
            Message::BlockingScalar(scalar) => Some(scalar),
        }
    }
    pub(crate) fn message_type(&self) -> usize {
        match *self {
            Message::MutableBorrow(_) => 1,
            Message::Borrow(_) => 2,
            Message::Move(_) => 3,
            Message::Scalar(_) => 4,
            Message::BlockingScalar(_) => 5,
        }
    }

    /// Return the ID of this message
    pub fn id(&self) -> MessageId {
        match self {
            Message::MutableBorrow(mem) | Message::Borrow(mem) | Message::Move(mem) => mem.id,
            Message::Scalar(s) | Message::BlockingScalar(s) => s.id,
        }
    }

    /// Set the ID or opcode of this message
    pub fn set_id(&mut self, id: MessageId) {
        match self {
            Message::MutableBorrow(mem) | Message::Borrow(mem) | Message::Move(mem) => mem.id = id,
            Message::Scalar(s) | Message::BlockingScalar(s) => s.id = id,
        }
    }

    pub fn to_usize(&self) -> [usize; 6] {
        let ret = match &*self {
            Message::MutableBorrow(m) => (0, m.to_usize()),
            Message::Borrow(m) => (1, m.to_usize()),
            Message::Move(m) => (2, m.to_usize()),
            Message::Scalar(m) => (3, m.to_usize()),
            Message::BlockingScalar(m) => (4, m.to_usize()),
        };
        [
            ret.0, ret.1[0], ret.1[1], ret.1[2], ret.1[3], ret.1[4],
        ]
    }
}

impl TryFrom<(usize, usize, usize, usize, usize, usize)> for Message {
    type Error = ();
    fn try_from(
        value: (usize, usize, usize, usize, usize, usize),
    ) -> core::result::Result<Self, Self::Error> {
        match value.0 {
            1 => Ok(Message::MutableBorrow(MemoryMessage {
                id: value.1,
                buf: unsafe { MemoryRange::new(value.2, value.3).map_err(|_| ()) }?,
                offset: MemoryAddress::new(value.4),
                valid: MemorySize::new(value.5),
            })),
            2 => Ok(Message::Borrow(MemoryMessage {
                id: value.1,
                buf: unsafe { MemoryRange::new(value.2, value.3).map_err(|_| ()) }?,
                offset: MemoryAddress::new(value.4),
                valid: MemorySize::new(value.5),
            })),
            3 => Ok(Message::Move(MemoryMessage {
                id: value.1,
                buf: unsafe { MemoryRange::new(value.2, value.3).map_err(|_| ()) }?,
                offset: MemoryAddress::new(value.4),
                valid: MemorySize::new(value.5),
            })),
            4 => Ok(Message::Scalar(ScalarMessage {
                id: value.1,
                arg1: value.2,
                arg2: value.3,
                arg3: value.4,
                arg4: value.5,
            })),
            5 => Ok(Message::BlockingScalar(ScalarMessage {
                id: value.1,
                arg1: value.2,
                arg2: value.3,
                arg3: value.4,
                arg4: value.5,
            })),
            _ => Err(()),
        }
    }
}