sel4/
syscalls.rs

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
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT
//

use core::array;

use sel4_config::{sel4_cfg, sel4_cfg_if};

use crate::{
    cap, cap_type, const_helpers::u32_into_usize, sys, Cap, CapType, ConveysReplyAuthority,
    InvocationContext, MessageInfo, Word, NUM_FAST_MESSAGE_REGISTERS,
};

#[sel4_cfg(not(KERNEL_MCS))]
use crate::IpcBuffer;

/// Number of message registers in the IPC buffer.
pub const NUM_MESSAGE_REGISTERS: usize = u32_into_usize(sys::seL4_MsgLimits::seL4_MsgMaxLength);

/// A capability badge.
pub type Badge = Word;

/// Trait for [`CapType`]s which are used as targets of IPC syscalls.
pub trait IpcCapType: CapType {}

impl IpcCapType for cap_type::Notification {}

impl IpcCapType for cap_type::Endpoint {}

// HACK
impl IpcCapType for cap_type::Unspecified {}

sel4_cfg_if! {
    if #[sel4_cfg(KERNEL_MCS)] {
        pub type WaitMessageInfo = MessageInfo;

        fn wait_message_info_from_sys(info: sys::WaitMessageInfo) -> WaitMessageInfo {
            MessageInfo::from_inner(info)
        }
    } else {
        pub type WaitMessageInfo = ();

        fn wait_message_info_from_sys(info: sys::WaitMessageInfo) -> WaitMessageInfo {
            info
        }
    }
}

impl<C: InvocationContext> cap::Endpoint<C> {
    /// Corresponds to `seL4_Send`.
    pub fn send(self, info: MessageInfo) {
        self.invoke(|cptr, ipc_buffer| {
            ipc_buffer
                .inner_mut()
                .seL4_Send(cptr.bits(), info.into_inner())
        })
    }

    /// Corresponds to `seL4_NBSend`.
    pub fn nb_send(self, info: MessageInfo) {
        self.invoke(|cptr, ipc_buffer| {
            ipc_buffer
                .inner_mut()
                .seL4_NBSend(cptr.bits(), info.into_inner())
        })
    }

    /// Corresponds to `seL4_Recv`.
    pub fn recv(self, reply_authority: impl ConveysReplyAuthority) -> (MessageInfo, Badge) {
        let (raw_msg_info, badge) = self.invoke(|cptr, ipc_buffer| {
            ipc_buffer.inner_mut().seL4_Recv(
                cptr.bits(),
                reply_authority
                    .into_reply_authority()
                    .into_sys_reply_authority(),
            )
        });
        (MessageInfo::from_inner(raw_msg_info), badge)
    }

    /// Corresponds to `seL4_NBRecv`.
    pub fn nb_recv(self, reply_authority: impl ConveysReplyAuthority) -> (MessageInfo, Badge) {
        let (raw_msg_info, badge) = self.invoke(|cptr, ipc_buffer| {
            ipc_buffer.inner_mut().seL4_NBRecv(
                cptr.bits(),
                reply_authority
                    .into_reply_authority()
                    .into_sys_reply_authority(),
            )
        });
        (MessageInfo::from_inner(raw_msg_info), badge)
    }

    /// Corresponds to `seL4_Call`.
    pub fn call(self, info: MessageInfo) -> MessageInfo {
        MessageInfo::from_inner(self.invoke(|cptr, ipc_buffer| {
            ipc_buffer
                .inner_mut()
                .seL4_Call(cptr.bits(), info.into_inner())
        }))
    }

    /// Corresponds to `seL4_ReplyRecv`.
    pub fn reply_recv(
        self,
        info: MessageInfo,
        reply_authority: impl ConveysReplyAuthority,
    ) -> (MessageInfo, Badge) {
        let (raw_msg_info, badge) = self.invoke(|cptr, ipc_buffer| {
            ipc_buffer.inner_mut().seL4_ReplyRecv(
                cptr.bits(),
                info.into_inner(),
                reply_authority
                    .into_reply_authority()
                    .into_sys_reply_authority(),
            )
        });
        (MessageInfo::from_inner(raw_msg_info), badge)
    }

    pub fn send_with_mrs<T: FastMessages>(self, info: MessageInfo, messages: T) {
        let [msg0, msg1, msg2, msg3] = messages.prepare_in();
        self.invoke(|cptr, ipc_buffer| {
            ipc_buffer.inner_mut().seL4_SendWithMRs(
                cptr.bits(),
                info.into_inner(),
                msg0,
                msg1,
                msg2,
                msg3,
            )
        })
    }

    pub fn recv_with_mrs(self, reply_authority: impl ConveysReplyAuthority) -> RecvWithMRs {
        let mut msg = [0; NUM_FAST_MESSAGE_REGISTERS];
        let [ref mut mr0, ref mut mr1, ref mut mr2, ref mut mr3] = &mut msg;
        let (raw_msg_info, badge) = self.invoke(|cptr, ipc_buffer| {
            ipc_buffer.inner_mut().seL4_RecvWithMRs(
                cptr.bits(),
                Some(mr0),
                Some(mr1),
                Some(mr2),
                Some(mr3),
                reply_authority
                    .into_reply_authority()
                    .into_sys_reply_authority(),
            )
        });
        RecvWithMRs {
            info: MessageInfo::from_inner(raw_msg_info),
            badge,
            msg,
        }
    }

    pub fn call_with_mrs<T: FastMessages>(self, info: MessageInfo, messages: T) -> CallWithMRs {
        let mut msg = messages.prepare_in_out();
        let [ref mut mr0, ref mut mr1, ref mut mr2, ref mut mr3] = &mut msg;
        let raw_msg_info = self.invoke(|cptr, ipc_buffer| {
            ipc_buffer.inner_mut().seL4_CallWithMRs(
                cptr.bits(),
                info.into_inner(),
                Some(mr0),
                Some(mr1),
                Some(mr2),
                Some(mr3),
            )
        });
        CallWithMRs {
            info: MessageInfo::from_inner(raw_msg_info),
            msg,
        }
    }
}

impl<C: InvocationContext> cap::Notification<C> {
    /// Corresponds to `seL4_Signal`.
    pub fn signal(self) {
        self.invoke(|cptr, ipc_buffer| ipc_buffer.inner_mut().seL4_Signal(cptr.bits()))
    }

    /// Corresponds to `seL4_Wait`.
    pub fn wait(self) -> (WaitMessageInfo, Badge) {
        let (info, badge) =
            self.invoke(|cptr, ipc_buffer| ipc_buffer.inner_mut().seL4_Wait(cptr.bits()));
        (wait_message_info_from_sys(info), badge)
    }
}

#[sel4_cfg(KERNEL_MCS)]
impl<C: InvocationContext> cap::Reply<C> {
    /// Corresponds to `seL4_Send`.
    pub fn send(self, info: MessageInfo) {
        self.invoke(|cptr, ipc_buffer| {
            ipc_buffer
                .inner_mut()
                .seL4_Send(cptr.bits(), info.into_inner())
        })
    }
}

// TODO more
impl<T: IpcCapType, C: InvocationContext> Cap<T, C> {
    /// Corresponds to `seL4_NBSendRecv`.
    #[sel4_cfg(KERNEL_MCS)]
    pub fn nb_send_recv<U: IpcCapType>(
        self,
        info: MessageInfo,
        src: Cap<U>,
        reply_authority: impl ConveysReplyAuthority,
    ) -> (MessageInfo, Badge) {
        let (raw_msg_info, badge) = self.invoke(|cptr, ipc_buffer| {
            ipc_buffer.inner_mut().seL4_NBSendRecv(
                cptr.bits(),
                info.into_inner(),
                src.bits(),
                reply_authority
                    .into_reply_authority()
                    .into_sys_reply_authority(),
            )
        });
        (MessageInfo::from_inner(raw_msg_info), badge)
    }
}

/// Corresponds to `seL4_Reply`.
#[sel4_cfg(not(KERNEL_MCS))]
pub fn reply(ipc_buffer: &mut IpcBuffer, info: MessageInfo) {
    ipc_buffer.inner_mut().seL4_Reply(info.into_inner())
}

/// Corresponds to `seL4_Yield`.
pub fn r#yield() {
    sys::seL4_Yield()
}

/// Corresponds to `seL4_SetTLSBase`.
#[sel4_cfg(SET_TLS_BASE_SELF)]
pub fn set_tls_base(addr: usize) {
    sys::seL4_SetTLSBase(addr.try_into().unwrap())
}

//

const UNUSED_FOR_IN: Word = 0;

/// The result of [`cap::Endpoint::recv_with_mrs`].
pub struct RecvWithMRs {
    pub info: MessageInfo,
    pub badge: Badge,
    pub msg: [Word; NUM_FAST_MESSAGE_REGISTERS],
}

/// The result of [`cap::Endpoint::call_with_mrs`].
pub struct CallWithMRs {
    pub info: MessageInfo,
    pub msg: [Word; NUM_FAST_MESSAGE_REGISTERS],
}

type ConcreteFastMessagesForIn = [Option<Word>; NUM_FAST_MESSAGE_REGISTERS];

type ConcreteFastMessagesForInOut = [Word; NUM_FAST_MESSAGE_REGISTERS];

/// Trait for types which can hold the contents of a set of inline message registers.
pub trait FastMessages: fast_messages_sealing::FastMessagesSealed {
    fn prepare_in(self) -> ConcreteFastMessagesForIn;

    fn prepare_in_out(self) -> ConcreteFastMessagesForInOut;
}

impl<const N: usize> FastMessages for [Word; N]
where
    [Word; N]: fast_messages_sealing::FastMessagesSealed,
{
    fn prepare_in(self) -> ConcreteFastMessagesForIn {
        array::from_fn(|i| if i < self.len() { Some(self[i]) } else { None })
    }

    fn prepare_in_out(self) -> ConcreteFastMessagesForInOut {
        array::from_fn(|i| {
            if i < self.len() {
                self[i]
            } else {
                UNUSED_FOR_IN
            }
        })
    }
}

impl FastMessages for &[Word] {
    fn prepare_in(self) -> ConcreteFastMessagesForIn {
        assert!(self.len() <= NUM_FAST_MESSAGE_REGISTERS);
        array::from_fn(|i| if i < self.len() { Some(self[i]) } else { None })
    }

    fn prepare_in_out(self) -> ConcreteFastMessagesForInOut {
        assert!(self.len() <= NUM_FAST_MESSAGE_REGISTERS);
        array::from_fn(|i| {
            if i < self.len() {
                self[i]
            } else {
                UNUSED_FOR_IN
            }
        })
    }
}

mod fast_messages_sealing {
    use super::Word;

    pub trait FastMessagesSealed {}

    impl FastMessagesSealed for [Word; 0] {}
    impl FastMessagesSealed for [Word; 1] {}
    impl FastMessagesSealed for [Word; 2] {}
    impl FastMessagesSealed for [Word; 3] {}
    impl FastMessagesSealed for [Word; 4] {}

    impl FastMessagesSealed for &[Word] {}
}

#[allow(clippy::assertions_on_constants)]
const _: () = assert!(NUM_FAST_MESSAGE_REGISTERS == 4);