init
This commit is contained in:
5
crates/types/src/address.rs
Normal file
5
crates/types/src/address.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct EntryPoint(usize);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StackPointer(usize);
|
||||
4
crates/types/src/bounded.rs
Normal file
4
crates/types/src/bounded.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
use crate::macros::bounded;
|
||||
|
||||
bounded!(CapSlot, u8, 0, 63);
|
||||
bounded!(Priority, u8, 0, 31);
|
||||
16
crates/types/src/generation.rs
Normal file
16
crates/types/src/generation.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Generation(u32);
|
||||
|
||||
impl Generation {
|
||||
pub fn initial() -> Self {
|
||||
Self(0)
|
||||
}
|
||||
|
||||
pub fn next(&self) -> Self {
|
||||
Self(self.0.wrapping_add(1))
|
||||
}
|
||||
|
||||
pub fn is_valid_against(&self, current: &Generation) -> bool {
|
||||
self == current
|
||||
}
|
||||
}
|
||||
8
crates/types/src/id.rs
Normal file
8
crates/types/src/id.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use crate::macros::kernel_id;
|
||||
|
||||
kernel_id!(ProcessId, u16);
|
||||
kernel_id!(ThreadId, u16);
|
||||
kernel_id!(EndpointId, u16);
|
||||
kernel_id!(RegionId, u16);
|
||||
kernel_id!(IrqId, u16);
|
||||
kernel_id!(CoreId, u8);
|
||||
9
crates/types/src/lib.rs
Normal file
9
crates/types/src/lib.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
#![no_std]
|
||||
|
||||
pub mod address;
|
||||
pub mod bounded;
|
||||
pub mod generation;
|
||||
pub mod id;
|
||||
pub mod macros;
|
||||
pub mod permission;
|
||||
pub mod time;
|
||||
43
crates/types/src/macros.rs
Normal file
43
crates/types/src/macros.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
macro_rules! kernel_id {
|
||||
($name:ident, $inner:ty) => {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct $name($inner);
|
||||
|
||||
impl $name {
|
||||
pub fn from_raw(value: $inner) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> $inner {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! bounded {
|
||||
($name:ident, $inner:ty, $min:expr, $max:expr) => {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct $name($inner);
|
||||
|
||||
impl $name {
|
||||
pub const MIN: $inner = $min;
|
||||
pub const MAX: $inner = $max;
|
||||
|
||||
pub fn new(value: $inner) -> Option<Self> {
|
||||
if value >= Self::MIN && value <= Self::MAX {
|
||||
Some(Self(value))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn value(&self) -> $inner {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use bounded;
|
||||
pub(crate) use kernel_id;
|
||||
43
crates/types/src/permission.rs
Normal file
43
crates/types/src/permission.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Permission {
|
||||
Read,
|
||||
Write,
|
||||
Execute,
|
||||
Delegate,
|
||||
Revoke,
|
||||
}
|
||||
|
||||
impl Permission {
|
||||
fn bit(&self) -> u8 {
|
||||
1 << (*self as u8)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct PermissionSet(u8);
|
||||
|
||||
impl PermissionSet {
|
||||
pub fn empty() -> Self {
|
||||
Self(0)
|
||||
}
|
||||
|
||||
pub fn with(self, p: Permission) -> Self {
|
||||
Self(self.0 | p.bit())
|
||||
}
|
||||
|
||||
pub fn without(self, p: Permission) -> Self {
|
||||
Self(self.0 & !p.bit())
|
||||
}
|
||||
|
||||
pub fn allows(&self, p: Permission) -> bool {
|
||||
self.0 & p.bit() != 0
|
||||
}
|
||||
|
||||
pub fn restrict_to(&self, other: &PermissionSet) -> Self {
|
||||
Self(self.0 & other.0)
|
||||
}
|
||||
|
||||
pub fn is_subset_of(&self, other: &PermissionSet) -> bool {
|
||||
self.0 & other.0 == self.0
|
||||
}
|
||||
}
|
||||
11
crates/types/src/time.rs
Normal file
11
crates/types/src/time.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Ticks(u64);
|
||||
|
||||
impl Ticks {
|
||||
pub fn elapsed_since(&self, earlier: &Ticks) -> Ticks {
|
||||
Ticks(self.0.saturating_sub(earlier.0))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TimerDuration(u32);
|
||||
Reference in New Issue
Block a user