Skip to content

Commit

Permalink
crates/sel4-supervising/types: Split out
Browse files Browse the repository at this point in the history
  • Loading branch information
nspin committed Jul 14, 2024
1 parent e0e5054 commit 3168768
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 92 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ members = [
"crates/sel4-shared-ring-buffer/smoltcp",
"crates/sel4-stack",
"crates/sel4-supervising",
"crates/sel4-supervising/types",
"crates/sel4-sync",
"crates/sel4-sync/trivial",
"crates/sel4-synthetic-elf",
Expand Down
5 changes: 2 additions & 3 deletions crates/sel4-supervising/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
# SPDX-License-Identifier: BSD-2-Clause
#

{ mk, versions, localCrates }:
{ mk, localCrates }:

mk {
package.name = "sel4-supervising";
dependencies = {
inherit (versions) zerocopy;
inherit (localCrates) sel4;
inherit (localCrates) sel4 sel4-supervising-types;
};
}
2 changes: 1 addition & 1 deletion crates/sel4-supervising/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ license = "BSD-2-Clause"

[dependencies]
sel4 = { path = "../sel4" }
zerocopy = "0.7.32"
sel4-supervising-types = { path = "types" }
92 changes: 4 additions & 88 deletions crates/sel4-supervising/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

use core::ops::{Range, RangeInclusive};

use zerocopy::AsBytes;

use sel4::{sel4_cfg, UnknownSyscall, UnknownSyscallInIpcBuffer, UserContext, Word};

pub use sel4_supervising_types::{
MemoryAccessData as VmFaultData, MemoryAccessWidth as VmFaultWidth,
};

mod arch;

pub use arch::*;
Expand Down Expand Up @@ -349,92 +351,6 @@ where
}
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub enum VmFaultWidth {
U8,
U16,
U32,
#[cfg(target_pointer_width = "64")]
U64,
}

impl VmFaultWidth {
pub fn mask(self) -> Word {
match self {
Self::U8 => 0xff,
Self::U16 => 0xffff,
Self::U32 => 0xffff_ffff,
#[cfg(target_pointer_width = "64")]
Self::U64 => 0xffff_ffff_ffff_ffff,
}
}

pub fn truncate(self, val: Word) -> VmFaultData {
match self {
Self::U8 => VmFaultData::U8(val as u8),
Self::U16 => VmFaultData::U16(val as u16),
Self::U32 => VmFaultData::U32(val as u32),
#[cfg(target_pointer_width = "64")]
Self::U64 => VmFaultData::U64(val as u64),
}
}
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum VmFaultData {
U8(u8),
U16(u16),
U32(u32),
#[cfg(target_pointer_width = "64")]
U64(u64),
}

impl VmFaultData {
pub fn width(&self) -> VmFaultWidth {
match self {
Self::U8(_) => VmFaultWidth::U8,
Self::U16(_) => VmFaultWidth::U16,
Self::U32(_) => VmFaultWidth::U32,
#[cfg(target_pointer_width = "64")]
Self::U64(_) => VmFaultWidth::U64,
}
}

pub fn zero_extend(&self) -> Word {
match self {
Self::U8(raw) => *raw as Word,
Self::U16(raw) => *raw as Word,
Self::U32(raw) => *raw as Word,
#[cfg(target_pointer_width = "64")]
Self::U64(raw) => *raw as Word,
}
}

pub fn bytes(&self) -> &[u8] {
match self {
Self::U8(raw) => raw.as_bytes(),
Self::U16(raw) => raw.as_bytes(),
Self::U32(raw) => raw.as_bytes(),
#[cfg(target_pointer_width = "64")]
Self::U64(raw) => raw.as_bytes(),
}
}

pub fn bytes_mut(&mut self) -> &mut [u8] {
match self {
Self::U8(raw) => raw.as_bytes_mut(),
Self::U16(raw) => raw.as_bytes_mut(),
Self::U32(raw) => raw.as_bytes_mut(),
#[cfg(target_pointer_width = "64")]
Self::U64(raw) => raw.as_bytes_mut(),
}
}

pub fn set(&self, old_val: Word) -> Word {
(old_val & !self.width().mask()) | self.zero_extend()
}
}

#[allow(dead_code)]
struct BitField {
start: u8,
Expand Down
14 changes: 14 additions & 0 deletions crates/sel4-supervising/types/Cargo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Copyright 2024, Colias Group, LLC
#
# SPDX-License-Identifier: BSD-2-Clause
#

{ mk, versions }:

mk {
package.name = "sel4-supervising-types";
dependencies = {
inherit (versions) zerocopy;
};
}
20 changes: 20 additions & 0 deletions crates/sel4-supervising/types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2023, Colias Group, LLC
#
# SPDX-License-Identifier: BSD-2-Clause
#
#
# This file is generated from './Cargo.nix'. You can edit this file directly
# if you are not using this project's Cargo manifest management tools.
# See 'hacking/cargo-manifest-management/README.md' for more information.
#

[package]
name = "sel4-supervising-types"
version = "0.1.0"
authors = ["Nick Spinale <[email protected]>"]
edition = "2021"
license = "BSD-2-Clause"

[dependencies]
zerocopy = "0.7.32"
101 changes: 101 additions & 0 deletions crates/sel4-supervising/types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

#![no_std]

use zerocopy::AsBytes;

#[cfg(target_pointer_width = "64")]
type Word = u64;

#[cfg(target_pointer_width = "32")]
type Word = u32;

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub enum MemoryAccessWidth {
U8,
U16,
U32,
#[cfg(target_pointer_width = "64")]
U64,
}

impl MemoryAccessWidth {
pub fn mask(self) -> Word {
match self {
Self::U8 => 0xff,
Self::U16 => 0xffff,
Self::U32 => 0xffff_ffff,
#[cfg(target_pointer_width = "64")]
Self::U64 => 0xffff_ffff_ffff_ffff,
}
}

pub fn truncate(self, val: Word) -> MemoryAccessData {
match self {
Self::U8 => MemoryAccessData::U8(val as u8),
Self::U16 => MemoryAccessData::U16(val as u16),
Self::U32 => MemoryAccessData::U32(val as u32),
#[cfg(target_pointer_width = "64")]
Self::U64 => MemoryAccessData::U64(val as u64),
}
}
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum MemoryAccessData {
U8(u8),
U16(u16),
U32(u32),
#[cfg(target_pointer_width = "64")]
U64(u64),
}

impl MemoryAccessData {
pub fn width(&self) -> MemoryAccessWidth {
match self {
Self::U8(_) => MemoryAccessWidth::U8,
Self::U16(_) => MemoryAccessWidth::U16,
Self::U32(_) => MemoryAccessWidth::U32,
#[cfg(target_pointer_width = "64")]
Self::U64(_) => MemoryAccessWidth::U64,
}
}

pub fn zero_extend(&self) -> Word {
match self {
Self::U8(raw) => *raw as Word,
Self::U16(raw) => *raw as Word,
Self::U32(raw) => *raw as Word,
#[cfg(target_pointer_width = "64")]
Self::U64(raw) => *raw as Word,
}
}

pub fn bytes(&self) -> &[u8] {
match self {
Self::U8(raw) => raw.as_bytes(),
Self::U16(raw) => raw.as_bytes(),
Self::U32(raw) => raw.as_bytes(),
#[cfg(target_pointer_width = "64")]
Self::U64(raw) => raw.as_bytes(),
}
}

pub fn bytes_mut(&mut self) -> &mut [u8] {
match self {
Self::U8(raw) => raw.as_bytes_mut(),
Self::U16(raw) => raw.as_bytes_mut(),
Self::U32(raw) => raw.as_bytes_mut(),
#[cfg(target_pointer_width = "64")]
Self::U64(raw) => raw.as_bytes_mut(),
}
}

pub fn set(&self, old_val: Word) -> Word {
(old_val & !self.width().mask()) | self.zero_extend()
}
}

0 comments on commit 3168768

Please sign in to comment.