Compare commits

...

1 commit

Author SHA1 Message Date
Elfein Landers ab39b02bb9 Refactor of scheduler's 2022-02-02 01:52:42 -08:00
5 changed files with 362 additions and 325 deletions

View file

@ -7,7 +7,7 @@ use {
arch::{init, sloop},
boot_conf,
boot_conf::BootConfig,
capabilities::FileAccess,
capabilities,
experiments::{
info::master,
systeminfo::{KERNEL_VERSION, RELEASE_TYPE},
@ -43,7 +43,7 @@ pub fn kernel_main() -> ! {
use Priority::*;
let mut process_1 = scheduler.new_process(High);
process_1.capabilities.files = FileAccess::Some(vec![PathRep {
process_1.capabilities.set_file_access(vec![PathRep {
location: FileLocations::Home,
file_name: "test".to_string(),
}]);

View file

@ -1,72 +1,61 @@
use core::ops::BitOr;
use alloc::vec::Vec;
use crate::file::PathRep;
// TODO: Get Able to document this
///
pub type SoundCardID = u8;
// TODO: Get Able to document this
///
pub type DeviceID = u8;
// TODO: Get Able to document this
///
pub type ControllerID = u8;
#[repr(u8)]
#[derive(Clone, Debug, PartialEq)]
pub enum FileAccess {
All,
Some(Vec<PathRep>),
None,
/// Enum that specifies which abilities a program can have.
pub enum Ability {
// TODO: Get Able to document this
///
AllControllers = 0x01,
// TODO: Get Able to document this
///
AllFiles = 0x02,
// TODO: Get Able to document this
///
AllSoundCards = 0x04,
// TODO: Get Able to document this
///
Keyboard = 0x08,
// TODO: Get Able to document this
///
Mouse = 0x10,
// TODO: Get Able to document this
///
Telecomms = 0x20,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ControllerAccess {
All,
Some(Vec<ControllerID>),
None,
}
#[derive(Clone, Debug, Default, PartialEq)]
/// Wrapper for bit mask of Ability.
pub struct Permissions(u8);
#[derive(Clone, Debug, PartialEq)]
pub enum SoundCardAccess {
All,
Some(Vec<SoundCardID>),
None,
}
#[derive(Clone, Debug, PartialEq)]
pub enum MouseAccess {
Yes,
No,
}
#[derive(Clone, Debug, PartialEq)]
pub enum KeyboardAccess {
Yes,
No,
}
#[derive(Clone, Debug, PartialEq)]
pub enum NetworkAccess {
Yes,
No,
}
/// A set of capabilities that a process has
#[derive(Clone, Debug, PartialEq)]
/// The capabilities of a program. This must only be handled by kernel code.
/// Capabilities should never be set by user code, either directly or indirectly.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Capabilities {
// TODO: Add more capabilities
pub files: FileAccess,
pub mouse: MouseAccess,
pub keyboard: KeyboardAccess,
pub controllers: ControllerAccess,
pub sound_cards: SoundCardAccess,
pub network_access: NetworkAccess,
perms: Permissions,
files: Vec<PathRep>,
controllers: Vec<ControllerID>,
soundcards: Vec<SoundCardID>,
}
impl Capabilities {
/// Generate a set of empty capabilities
pub fn empty() -> Self {
Self {
files: FileAccess::None,
mouse: MouseAccess::No,
keyboard: KeyboardAccess::No,
controllers: ControllerAccess::None,
sound_cards: SoundCardAccess::None,
network_access: NetworkAccess::No,
}
Self::default()
}
/// Generate a set of capabilities that allows all access
@ -77,12 +66,64 @@ impl Capabilities {
/// unless you know what you are doing
pub unsafe fn all() -> Self {
Self {
files: FileAccess::All,
mouse: MouseAccess::Yes,
keyboard: KeyboardAccess::Yes,
controllers: ControllerAccess::All,
sound_cards: SoundCardAccess::All,
network_access: NetworkAccess::Yes,
perms: Permissions::all(),
..Default::default()
}
}
/// Checks if capabilities has this ability.
pub fn has_ability(&self, ability: Ability) -> bool {
self.perms.has_ability(ability)
}
/// Sets which individual files this capabilities grants access to.
pub fn set_file_access(&mut self, files: Vec<PathRep>) {
self.files = files;
}
/// Sets which individual controllers this capabilities grants access to.
pub fn set_controller_access(&mut self, controllers: Vec<ControllerID>) {
self.controllers = controllers;
}
/// Sets which individual soundcards this capabilities grants access to.
pub fn set_soundcard_access(&mut self, soundcards: Vec<SoundCardID>) {
self.soundcards = soundcards;
}
}
impl Ability {
/// Returns a bit mask with all abilities enabled.
pub fn all() -> u8 {
use Ability::*;
AllControllers | AllFiles | AllSoundCards | Keyboard | Mouse | Telecomms
}
}
impl BitOr for Ability {
type Output = u8;
fn bitor(self, rhs: Self) -> u8 {
self as u8 | rhs as u8
}
}
impl BitOr<u8> for Ability {
type Output = u8;
fn bitor(self, rhs: u8) -> u8 {
self as u8 | rhs
}
}
impl BitOr<Ability> for u8 {
type Output = u8;
fn bitor(self, rhs: Ability) -> u8 {
self | rhs as u8
}
}
impl Permissions {
/// Returns self with all abilities enabled.
pub fn all() -> Self {
Self(Ability::all())
}
/// Checks if permissions has ability.
pub fn has_ability(&self, ability: Ability) -> bool {
self.0 & ability.clone() as u8 == ability as u8
}
}

View file

@ -19,7 +19,7 @@ mod tests;
use crate::file::PathRep;
use self::capabilities::Capabilities;
use capabilities::Capabilities;
lazy_static::lazy_static!(
/// The standard implementation for ableOS

View file

@ -1,11 +1,6 @@
use super::*;
use crate::scheduler::{
capabilities::{ControllerAccess, FileAccess, KeyboardAccess, MouseAccess, SoundCardAccess},
proc::PID,
Priority::*,
Scheduler,
};
use crate::{scheduler::{proc::PID, Priority::*, Scheduler}, capabilities::Ability};
// #[test]
fn test_new_process() {
@ -41,9 +36,10 @@ fn test_bump_exec() {
// #[test]
fn test_capabilities() {
let caps = Capabilities::empty();
assert_eq!(caps.files, FileAccess::None);
assert_eq!(caps.mouse, MouseAccess::No);
assert_eq!(caps.keyboard, KeyboardAccess::No);
assert_eq!(caps.controllers, ControllerAccess::None);
assert_eq!(caps.sound_cards, SoundCardAccess::None);
assert!(!caps.has_ability(Ability::AllControllers));
assert!(!caps.has_ability(Ability::AllFiles));
assert!(!caps.has_ability(Ability::AllSoundCards));
assert!(!caps.has_ability(Ability::Keyboard));
assert!(!caps.has_ability(Ability::Mouse));
assert!(!caps.has_ability(Ability::Telecomms));
}