2020-03-10 04:04:17 +00:00
|
|
|
use core::num::NonZeroUsize;
|
|
|
|
|
|
|
|
|
|
pub type MemoryAddress = NonZeroUsize;
|
|
|
|
|
pub type MemorySize = NonZeroUsize;
|
|
|
|
|
pub type StackPointer = usize;
|
|
|
|
|
pub type MessageId = usize;
|
|
|
|
|
|
|
|
|
|
pub type PID = u8;
|
|
|
|
|
pub type MessageSender = usize;
|
|
|
|
|
pub type Connection = usize;
|
|
|
|
|
|
|
|
|
|
/// Server ID
|
2020-03-14 07:57:58 +00:00
|
|
|
pub type SID = (usize, usize, usize, usize);
|
2020-03-10 04:04:17 +00:00
|
|
|
|
2020-03-17 05:00:06 +00:00
|
|
|
/// Connection ID
|
|
|
|
|
pub type CID = usize;
|
|
|
|
|
|
2020-03-27 05:32:41 +00:00
|
|
|
/// Context ID
|
|
|
|
|
pub type CtxID = usize;
|
|
|
|
|
|
2020-03-10 04:04:17 +00:00
|
|
|
/// Equivalent to a RISC-V Hart ID
|
|
|
|
|
pub type CpuID = usize;
|
|
|
|
|
|
2020-04-01 05:21:23 +00:00
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
2020-04-01 02:28:36 +00:00
|
|
|
pub struct MemoryRange {
|
|
|
|
|
pub addr: MemoryAddress,
|
|
|
|
|
pub size: MemorySize,
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 04:28:44 +00:00
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
|
/// Flags to be passed to the MapMemory struct.
|
|
|
|
|
/// Note that it is an error to have memory be
|
|
|
|
|
/// writable and not readable.
|
|
|
|
|
pub struct MemoryFlags: usize {
|
|
|
|
|
/// Free this memory
|
|
|
|
|
const FREE = 0b00000000;
|
|
|
|
|
|
|
|
|
|
/// Immediately allocate this memory. Otherwise it will
|
|
|
|
|
/// be demand-paged. This is implicitly set when `phys`
|
|
|
|
|
/// is not 0.
|
|
|
|
|
const RESERVE = 0b00000001;
|
|
|
|
|
|
|
|
|
|
/// Allow the CPU to read from this page.
|
|
|
|
|
const R = 0b00000010;
|
|
|
|
|
|
|
|
|
|
/// Allow the CPU to write to this page.
|
|
|
|
|
const W = 0b00000100;
|
|
|
|
|
|
|
|
|
|
/// Allow the CPU to execute from this page.
|
|
|
|
|
const X = 0b00001000;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 02:27:03 +00:00
|
|
|
#[repr(usize)]
|
2020-03-10 04:04:17 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
|
pub enum Error {
|
|
|
|
|
NoError = 0,
|
|
|
|
|
BadAlignment = 1,
|
|
|
|
|
BadAddress = 2,
|
|
|
|
|
OutOfMemory = 3,
|
|
|
|
|
MemoryInUse = 4,
|
|
|
|
|
InterruptNotFound = 5,
|
|
|
|
|
InterruptInUse = 6,
|
|
|
|
|
InvalidString = 7,
|
|
|
|
|
ServerExists = 8,
|
|
|
|
|
ServerNotFound = 9,
|
|
|
|
|
ProcessNotFound = 10,
|
|
|
|
|
ProcessNotChild = 11,
|
|
|
|
|
ProcessTerminated = 12,
|
|
|
|
|
Timeout = 13,
|
2020-03-13 12:33:08 +00:00
|
|
|
InternalError = 14,
|
2020-03-24 07:26:25 +00:00
|
|
|
ServerQueueFull = 15,
|
2020-03-27 05:32:41 +00:00
|
|
|
ContextNotAvailable = 16,
|
|
|
|
|
UnhandledSyscall = 17,
|
2020-04-01 02:28:36 +00:00
|
|
|
InvalidSyscall = 18,
|
2020-04-03 04:28:44 +00:00
|
|
|
ShareViolation = 19,
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub struct Context {
|
|
|
|
|
stack: StackPointer,
|
|
|
|
|
pid: PID,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[repr(C)]
|
2020-03-19 09:26:01 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-19 00:52:25 +00:00
|
|
|
/// A struct describing memory that is passed between processes.
|
|
|
|
|
/// The `buf` value will get translated as necessary.
|
2020-03-10 04:04:17 +00:00
|
|
|
pub struct MemoryMessage {
|
2020-03-19 00:52:25 +00:00
|
|
|
/// A user-assignable message ID.
|
2020-03-17 05:00:06 +00:00
|
|
|
pub id: MessageId,
|
2020-03-19 00:52:25 +00:00
|
|
|
|
2020-04-01 02:28:36 +00:00
|
|
|
/// The offset of the buffer. This address will get transformed when the
|
|
|
|
|
/// message is moved between processes.
|
|
|
|
|
pub buf: MemoryRange,
|
2020-03-19 00:52:25 +00:00
|
|
|
|
2020-04-01 02:28:36 +00:00
|
|
|
/// The offset within the buffer where the interesting stuff starts.
|
2020-03-31 10:03:19 +00:00
|
|
|
pub offset: Option<MemorySize>,
|
2020-03-19 00:52:25 +00:00
|
|
|
|
|
|
|
|
/// How many bytes in the buffer are valid
|
2020-03-31 10:03:19 +00:00
|
|
|
pub valid: Option<MemorySize>,
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[repr(C)]
|
2020-03-19 09:26:01 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-19 00:52:25 +00:00
|
|
|
/// A simple scalar message. This is similar to a `move` message.
|
2020-03-10 04:04:17 +00:00
|
|
|
pub struct ScalarMessage {
|
2020-03-17 05:00:06 +00:00
|
|
|
pub id: MessageId,
|
|
|
|
|
pub arg1: usize,
|
|
|
|
|
pub arg2: usize,
|
|
|
|
|
pub arg3: usize,
|
|
|
|
|
pub arg4: usize,
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-17 02:27:03 +00:00
|
|
|
#[repr(usize)]
|
2020-03-19 09:26:01 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-10 04:04:17 +00:00
|
|
|
pub enum Message {
|
2020-03-19 00:52:25 +00:00
|
|
|
MutableBorrow(MemoryMessage),
|
|
|
|
|
ImmutableBorrow(MemoryMessage),
|
|
|
|
|
Move(MemoryMessage),
|
2020-03-10 04:04:17 +00:00
|
|
|
Scalar(ScalarMessage),
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 02:27:03 +00:00
|
|
|
#[repr(C)]
|
2020-03-20 09:22:02 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-03-17 02:27:03 +00:00
|
|
|
pub struct MessageEnvelope {
|
2020-03-20 09:22:02 +00:00
|
|
|
pub sender: MessageSender,
|
|
|
|
|
pub message: Message,
|
2020-03-10 04:04:17 +00:00
|
|
|
}
|
2020-04-01 02:28:36 +00:00
|
|
|
|
2020-04-01 05:21:23 +00:00
|
|
|
#[cfg(not(feature = "forget-memory-messages"))]
|
|
|
|
|
/// When a MessageEnvelope goes out of scope, return the memory. It must either
|
|
|
|
|
/// go to the kernel (in the case of a Move), or back to the borrowed process
|
|
|
|
|
/// (in the case of a Borrow). Ignore Scalar messages.
|
|
|
|
|
impl Drop for MessageEnvelope {
|
|
|
|
|
fn drop(&mut self) {
|
2020-04-01 09:27:59 +00:00
|
|
|
crate::syscall::return_memory(self.sender).expect("couldn't return memory");
|
|
|
|
|
if let Message::Move(msg) = &self.message {
|
|
|
|
|
crate::syscall::unmap_memory(msg.buf.addr, msg.buf.size)
|
2020-04-01 05:21:23 +00:00
|
|
|
.expect("couldn't free memory message");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 02:28:36 +00:00
|
|
|
impl MemoryRange {
|
|
|
|
|
pub fn new(addr: usize, size: usize) -> MemoryRange {
|
|
|
|
|
assert!(
|
|
|
|
|
addr != 0,
|
|
|
|
|
"tried to construct a memory range with a null pointer"
|
|
|
|
|
);
|
|
|
|
|
MemoryRange {
|
|
|
|
|
addr: MemoryAddress::new(addr).unwrap(),
|
|
|
|
|
size: MemorySize::new(size).unwrap(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.size.get()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn as_ptr(&self) -> *const usize {
|
|
|
|
|
self.addr.get() as *const usize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn as_mut_ptr(&self) -> *mut usize {
|
|
|
|
|
self.addr.get() as *mut usize
|
|
|
|
|
}
|
|
|
|
|
}
|