poplar/syscall/
mod.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
pub mod get_framebuffer;
pub mod pci;
pub mod result;

use core::mem::MaybeUninit;

pub use get_framebuffer::{get_framebuffer, FramebufferInfo, GetFramebufferError, PixelFormat};
pub use pci::{pci_get_info, PciGetInfoError};

cfg_if::cfg_if! {
    if #[cfg(target_arch = "x86_64")] {
        pub mod raw_x86_64;
        pub use raw_x86_64 as raw;
    } else if #[cfg(target_arch = "riscv64")] {
        pub mod raw_riscv;
        pub use raw_riscv as raw;
    } else {
        compile_error!("Poplar does not support this target architecture!");
    }
}

use crate::Handle;
use bit_field::BitField;
use result::{define_error_type, handle_from_syscall_repr, status_from_syscall_repr};

pub const SYSCALL_YIELD: usize = 0;
pub const SYSCALL_EARLY_LOG: usize = 1;
pub const SYSCALL_GET_FRAMEBUFFER: usize = 2;
pub const SYSCALL_CREATE_MEMORY_OBJECT: usize = 3;
pub const SYSCALL_MAP_MEMORY_OBJECT: usize = 4;
pub const SYSCALL_CREATE_CHANNEL: usize = 5;
pub const SYSCALL_SEND_MESSAGE: usize = 6;
pub const SYSCALL_GET_MESSAGE: usize = 7;
pub const SYSCALL_WAIT_FOR_MESSAGE: usize = 8;
pub const SYSCALL_REGISTER_SERVICE: usize = 9;
pub const SYSCALL_SUBSCRIBE_TO_SERVICE: usize = 10;
pub const SYSCALL_PCI_GET_INFO: usize = 11;
pub const SYSCALL_WAIT_FOR_EVENT: usize = 12;
pub const SYSCALL_POLL_INTEREST: usize = 13;

pub fn yield_to_kernel() {
    unsafe {
        raw::syscall0(SYSCALL_YIELD);
    }
}

define_error_type!(EarlyLogError {
    MessageTooLong => 1,
    MessageNotValidUtf8 => 2,
    TaskDoesNotHaveCorrectCapability => 3,
});

pub fn early_log(message: &str) -> Result<(), EarlyLogError> {
    status_from_syscall_repr(unsafe {
        raw::syscall2(SYSCALL_EARLY_LOG, message.len(), message as *const str as *const u8 as usize)
    })
}

define_error_type!(CreateMemoryObjectError {
    InvalidFlags => 1,
    InvalidSize => 2,
    InvalidPhysicalAddressPointer => 3,
});

bitflags::bitflags! {
    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    pub struct MemoryObjectFlags: u32 {
        const WRITABLE = 1 << 0;
        const EXECUTABLE = 1 << 1;
    }
}

/// Create a MemoryObject kernel object of the given size (in bytes). Returns a handle to the new
/// MemoryObject, if the call was successful.
pub unsafe fn create_memory_object(
    size: usize,
    flags: MemoryObjectFlags,
    physical_address_ptr: *mut usize,
) -> Result<Handle, CreateMemoryObjectError> {
    handle_from_syscall_repr(unsafe {
        raw::syscall3(SYSCALL_CREATE_MEMORY_OBJECT, size, flags.bits() as usize, physical_address_ptr as usize)
    })
}

define_error_type!(MapMemoryObjectError {
    InvalidMemoryObjectHandle => 1,
    InvalidAddressSpaceHandle => 2,
    RegionAlreadyMapped => 3,
    AddressPointerInvalid => 4,
});

pub unsafe fn map_memory_object(
    memory_object: Handle,
    address_space: Handle,
    virtual_address: Option<usize>,
    address_pointer: *mut usize,
) -> Result<(), MapMemoryObjectError> {
    status_from_syscall_repr(unsafe {
        raw::syscall4(
            SYSCALL_MAP_MEMORY_OBJECT,
            memory_object.0 as usize,
            address_space.0 as usize,
            if virtual_address.is_some() { virtual_address.unwrap() } else { 0x0 },
            address_pointer as usize,
        )
    })
}

define_error_type!(CreateChannelError {
    InvalidHandleAddress => 1,
});

pub fn create_channel() -> Result<(Handle, Handle), CreateChannelError> {
    let mut other_end: MaybeUninit<Handle> = MaybeUninit::uninit();
    let one_end = handle_from_syscall_repr(unsafe {
        raw::syscall1(SYSCALL_CREATE_CHANNEL, other_end.as_mut_ptr() as usize)
    })?;
    Ok((one_end, unsafe { other_end.assume_init() }))
}

pub const CHANNEL_MAX_NUM_BYTES: usize = 4096;
pub const CHANNEL_MAX_NUM_HANDLES: usize = 4;

define_error_type!(SendMessageError {
    /// The `Channel` handle is invalid.
    InvalidChannelHandle => 1,
    /// The `Channel` handle isn't a `Channel`.
    NotAChannel => 2,
    /// The `Channel` handle must have the `SEND` right to use the `send_message` system call.
    ChannelCannotSend => 3,
    /// A handle to be transferred is invalid.
    InvalidTransferredHandle => 4,
    /// Transferred handles must have the `TRANSFER` right.
    CannotTransferHandle => 5,
    BytesAddressInvalid => 6,
    TooManyBytes => 7,
    HandlesAddressInvalid => 8,
    TooManyHandles => 9,
    OtherEndDisconnected => 10,
});

pub fn send_message(channel: Handle, bytes: &[u8], handles: &[Handle]) -> Result<(), SendMessageError> {
    status_from_syscall_repr(unsafe {
        raw::syscall5(
            SYSCALL_SEND_MESSAGE,
            channel.0 as usize,
            if bytes.len() == 0 { 0x0 } else { bytes.as_ptr() as usize },
            bytes.len(),
            if handles.len() == 0 { 0x0 } else { handles.as_ptr() as usize },
            handles.len(),
        )
    })
}

define_error_type!(GetMessageError {
    InvalidChannelHandle => 1,
    NotAChannel => 2,
    NoMessage => 3,
    BytesAddressInvalid => 4,
    BytesBufferTooSmall => 5,
    HandlesAddressInvalid => 6,
    HandlesBufferTooSmall => 7,
});

pub fn get_message<'b, 'h>(
    channel: Handle,
    byte_buffer: &'b mut [u8],
    handle_buffer: &'h mut [Handle],
) -> Result<(&'b mut [u8], &'h mut [Handle]), GetMessageError> {
    let result = unsafe {
        raw::syscall5(
            SYSCALL_GET_MESSAGE,
            channel.0 as usize,
            if byte_buffer.len() == 0 { 0x0 } else { byte_buffer.as_ptr() as usize },
            byte_buffer.len(),
            if handle_buffer.len() == 0 { 0x0 } else { handle_buffer.as_ptr() as usize },
            handle_buffer.len(),
        )
    };
    status_from_syscall_repr(result.get_bits(0..16))?;

    let valid_bytes_len = result.get_bits(16..32);
    let valid_handles_len = result.get_bits(32..48);

    Ok((&mut byte_buffer[0..valid_bytes_len], &mut handle_buffer[0..valid_handles_len]))
}

pub const SERVICE_NAME_MAX_LENGTH: usize = 256;

define_error_type!(RegisterServiceError {
    TaskDoesNotHaveCorrectCapability => 1,
    NamePointerNotValid => 2,
    /// Name must be greater than `0` bytes, and not greater than `256` bytes.
    NameLengthNotValid => 3,
});

pub fn register_service(name: &str) -> Result<Handle, RegisterServiceError> {
    handle_from_syscall_repr(unsafe {
        raw::syscall2(SYSCALL_REGISTER_SERVICE, name.len(), name.as_ptr() as usize)
    })
}

define_error_type!(SubscribeToServiceError {
    TaskDoesNotHaveCorrectCapability => 1,
    NamePointerNotValid => 2,
    /// Name must be greater than `0` bytes, and not greater than `256` bytes.
    NameLengthNotValid => 3,
    NoServiceWithThatName => 4,
});

pub fn subscribe_to_service(name: &str) -> Result<Handle, SubscribeToServiceError> {
    handle_from_syscall_repr(unsafe {
        raw::syscall2(SYSCALL_SUBSCRIBE_TO_SERVICE, name.len(), name.as_ptr() as usize)
    })
}

define_error_type!(WaitForEventError {
    InvalidHandle => 1,
    NotAnEvent => 2,
    /// No event has occured, and the caller does not want the kernel to block.
    NoEvent => 3,
});

pub fn wait_for_event(event: Handle, block: bool) -> Result<(), WaitForEventError> {
    let result = unsafe { raw::syscall2(SYSCALL_WAIT_FOR_EVENT, event.0 as usize, if block { 1 } else { 0 }) };
    status_from_syscall_repr(result)
}

define_error_type!(PollInterestError {
    InvalidHandle => 1,
});

pub fn poll_interest(object: Handle) -> Result<bool, PollInterestError> {
    let result = unsafe { raw::syscall1(SYSCALL_POLL_INTEREST, object.0 as usize) };
    status_from_syscall_repr(result.get_bits(0..16))?;
    Ok(result.get_bits(16..64) != 0)
}