Skip to content

Commit

Permalink
supervising
Browse files Browse the repository at this point in the history
  • Loading branch information
nspin committed Jul 14, 2024
1 parent e7168a3 commit 31065ed
Show file tree
Hide file tree
Showing 30 changed files with 1,241 additions and 351 deletions.
11 changes: 11 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 @@ -136,6 +136,7 @@ members = [
"crates/sel4-shared-ring-buffer/bookkeeping",
"crates/sel4-shared-ring-buffer/smoltcp",
"crates/sel4-stack",
"crates/sel4-supervising",
"crates/sel4-sync",
"crates/sel4-sync/trivial",
"crates/sel4-synthetic-elf",
Expand Down
1 change: 1 addition & 0 deletions crates/examples/root-task/spawn-task/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mk {
inherit (localCrates)
sel4
sel4-root-task
sel4-supervising
;
};
}
1 change: 1 addition & 0 deletions crates/examples/root-task/spawn-task/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ license = "BSD-2-Clause"
object = { version = "0.36.1", default-features = false, features = ["read"] }
sel4 = { path = "../../../sel4" }
sel4-root-task = { path = "../../../sel4-root-task" }
sel4-supervising = { path = "../../../sel4-supervising" }
3 changes: 2 additions & 1 deletion crates/examples/root-task/spawn-task/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use core::ptr;
use object::{File, Object};

use sel4_root_task::{root_task, Never};
use sel4_supervising::UserContextExt;

mod child_vspace;
mod object_allocator;
Expand Down Expand Up @@ -82,7 +83,7 @@ fn main(bootinfo: &sel4::BootInfoPtr) -> sel4::Result<Never> {
.unwrap();

let mut ctx = sel4::UserContext::default();
*ctx.pc_mut() = child_image.entry().try_into().unwrap();
*ctx.generic_pc_mut() = child_image.entry().try_into().unwrap();
child_tcb.tcb_write_all_registers(true, &mut ctx).unwrap();

inter_task_nfn.wait();
Expand Down
1 change: 1 addition & 0 deletions crates/examples/root-task/spawn-thread/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mk {
sel4-elf-header
sel4-stack
sel4-initialize-tls
sel4-supervising
;
};
}
1 change: 1 addition & 0 deletions crates/examples/root-task/spawn-thread/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ sel4-elf-header = { path = "../../../sel4-elf-header" }
sel4-initialize-tls = { path = "../../../sel4-initialize-tls" }
sel4-root-task = { path = "../../../sel4-root-task" }
sel4-stack = { path = "../../../sel4-stack" }
sel4-supervising = { path = "../../../sel4-supervising" }
5 changes: 3 additions & 2 deletions crates/examples/root-task/spawn-thread/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use sel4_root_task::{
abort, panicking::catch_unwind, root_task, set_global_allocator_mutex_notification, Never,
};
use sel4_stack::Stack;
use sel4_supervising::UserContextExt;

static SECONDARY_THREAD_STACK: Stack<4096> = Stack::new();

Expand Down Expand Up @@ -134,10 +135,10 @@ fn find_largest_kernel_untyped(bootinfo: &sel4::BootInfo) -> sel4::cap::Untyped
fn create_user_context(f: SecondaryThreadFn) -> sel4::UserContext {
let mut ctx = sel4::UserContext::default();

*ctx.sp_mut() = (SECONDARY_THREAD_STACK.bottom().ptr() as usize)
*ctx.generic_sp_mut() = (SECONDARY_THREAD_STACK.bottom().ptr() as usize)
.try_into()
.unwrap();
*ctx.pc_mut() = (secondary_thread_entrypoint as usize).try_into().unwrap();
*ctx.generic_pc_mut() = (secondary_thread_entrypoint as usize).try_into().unwrap();
*ctx.c_param_mut(0) = f.into_arg();

let tls_reservation = TlsReservation::new(&get_tls_image());
Expand Down
1 change: 1 addition & 0 deletions crates/sel4-capdl-initializer/core/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mk {
inherit (versions) log;
inherit (localCrates)
sel4
sel4-supervising
;
sel4-capdl-initializer-types = localCrates.sel4-capdl-initializer-types // { features = [ "sel4" ]; };
};
Expand Down
1 change: 1 addition & 0 deletions crates/sel4-capdl-initializer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ license = "BSD-2-Clause"
log = "0.4.17"
sel4 = { path = "../../sel4" }
sel4-capdl-initializer-types = { path = "../types", features = ["sel4"] }
sel4-supervising = { path = "../../sel4-supervising" }
5 changes: 3 additions & 2 deletions crates/sel4-capdl-initializer/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use sel4::{
CapRights, CapTypeForFrameObjectOfFixedSize,
};
use sel4_capdl_initializer_types::*;
use sel4_supervising::UserContextExt;

mod buffers;
mod cslot_allocator;
Expand Down Expand Up @@ -728,8 +729,8 @@ impl<'a, N: ObjectName, D: Content, M: GetEmbeddedFrame, B: BorrowMut<[PerObject

{
let mut regs = sel4::UserContext::default();
*regs.pc_mut() = obj.extra.ip;
*regs.sp_mut() = obj.extra.sp;
*regs.generic_pc_mut() = obj.extra.ip;
*regs.generic_sp_mut() = obj.extra.sp;
for (i, value) in obj.extra.gprs.iter().enumerate() {
*regs.c_param_mut(i) = *value;
}
Expand Down
15 changes: 15 additions & 0 deletions crates/sel4-supervising/Cargo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright 2024, Colias Group, LLC
#
# SPDX-License-Identifier: BSD-2-Clause
#

{ mk, versions, localCrates }:

mk {
package.name = "sel4-supervising";
dependencies = {
inherit (versions) zerocopy;
inherit (localCrates) sel4;
};
}
21 changes: 21 additions & 0 deletions crates/sel4-supervising/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# 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"
version = "0.1.0"
authors = ["Nick Spinale <[email protected]>"]
edition = "2021"
license = "BSD-2-Clause"

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

use sel4::VmFault;

use crate::{inner_decls, self_impl};

self_impl!(VmFaultExt, VmFault);

pub trait VmFaultExt {
inner_decls!(VmFault);
}
167 changes: 167 additions & 0 deletions crates/sel4-supervising/src/arch/aarch64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use sel4::{UserContext, VCpuFault, VmFault, Word};

use crate::{inner_decls, self_impl, BitField, VmFaultData, VmFaultWidth};

#[allow(dead_code)]
#[allow(non_upper_case_globals)]
mod esr {
use super::BitField;

pub(crate) const EC: BitField = BitField::from_range_inclusive(26..=31);
pub(crate) const ISV: BitField = BitField::bit(24);
pub(crate) const SAS: BitField = BitField::from_range_inclusive(22..=23);
pub(crate) const SSE: BitField = BitField::bit(21);
pub(crate) const SRT: BitField = BitField::from_range_inclusive(16..=20);
pub(crate) const SF: BitField = BitField::bit(15);
pub(crate) const WnR: BitField = BitField::bit(6);
}

self_impl!(VmFaultExt, VmFault);

pub trait VmFaultExt {
inner_decls!(VmFault);

fn esr_is_valid(&self) -> bool {
esr::ISV.get(self.inner().fsr()) == 1
}

fn valid_esr(&self) -> Word {
assert!(self.esr_is_valid());
self.inner().fsr()
}

fn is_write(&self) -> bool {
esr::WnR.get(self.valid_esr()) == 1
}

fn is_read(&self) -> bool {
!self.is_write()
}

fn width(&self) -> VmFaultWidth {
match esr::SAS.get(self.valid_esr()) {
0b00 => VmFaultWidth::U8,
0b01 => VmFaultWidth::U16,
0b10 => VmFaultWidth::U32,
0b11 => VmFaultWidth::U64,
_ => unreachable!(),
}
}

fn is_aligned(&self) -> bool {
let mask = match self.width() {
VmFaultWidth::U8 => 0x0,
VmFaultWidth::U16 => 0x1,
VmFaultWidth::U32 => 0x3,
VmFaultWidth::U64 => 0x7,
};
self.inner().addr() & mask == 0
}

fn data(&self, ctx: &UserContext) -> VmFaultData {
assert!(self.is_write());
self.width().truncate(self.register_value(ctx))
}

fn emulate_read(&self, ctx: &mut UserContext, val: VmFaultData) {
assert!(self.is_read());
assert_eq!(self.width(), val.width());
let reg = self.register_mut(ctx);
*reg = val.set(*reg);
}

fn register_value(&self, ctx: &UserContext) -> Word {
match register_index(self.inner()) {
0 => *ctx.x0(),
1 => *ctx.x1(),
2 => *ctx.x2(),
3 => *ctx.x3(),
4 => *ctx.x4(),
5 => *ctx.x5(),
6 => *ctx.x6(),
7 => *ctx.x7(),
8 => *ctx.x8(),
9 => *ctx.x9(),
10 => *ctx.x10(),
11 => *ctx.x11(),
12 => *ctx.x12(),
13 => *ctx.x13(),
14 => *ctx.x14(),
15 => *ctx.x15(),
16 => *ctx.x16(),
17 => *ctx.x17(),
18 => *ctx.x18(),
19 => *ctx.x19(),
20 => *ctx.x20(),
21 => *ctx.x21(),
22 => *ctx.x22(),
23 => *ctx.x23(),
24 => *ctx.x24(),
25 => *ctx.x25(),
26 => *ctx.x26(),
27 => *ctx.x27(),
28 => *ctx.x28(),
29 => *ctx.x29(),
30 => *ctx.x30(),
31 => 0,
_ => panic!(),
}
}

fn register_mut<'a>(&self, ctx: &'a mut UserContext) -> &'a mut Word {
match register_index(self.inner()) {
0 => ctx.x0_mut(),
1 => ctx.x1_mut(),
2 => ctx.x2_mut(),
3 => ctx.x3_mut(),
4 => ctx.x4_mut(),
5 => ctx.x5_mut(),
6 => ctx.x6_mut(),
7 => ctx.x7_mut(),
8 => ctx.x8_mut(),
9 => ctx.x9_mut(),
10 => ctx.x10_mut(),
11 => ctx.x11_mut(),
12 => ctx.x12_mut(),
13 => ctx.x13_mut(),
14 => ctx.x14_mut(),
15 => ctx.x15_mut(),
16 => ctx.x16_mut(),
17 => ctx.x17_mut(),
18 => ctx.x18_mut(),
19 => ctx.x19_mut(),
20 => ctx.x20_mut(),
21 => ctx.x21_mut(),
22 => ctx.x22_mut(),
23 => ctx.x23_mut(),
24 => ctx.x24_mut(),
25 => ctx.x25_mut(),
26 => ctx.x26_mut(),
27 => ctx.x27_mut(),
28 => ctx.x28_mut(),
29 => ctx.x29_mut(),
30 => ctx.x30_mut(),
_ => panic!(),
}
}
}

fn register_index(vm_fault: &VmFault) -> Word {
esr::SRT.get(vm_fault.valid_esr())
}

self_impl!(VCpuFaultExt, VCpuFault);

pub trait VCpuFaultExt {
inner_decls!(VCpuFault);

fn is_wfx(&self) -> bool {
esr::EC.get(self.inner().hsr()) == 0b000001
}
}
34 changes: 34 additions & 0 deletions crates/sel4-supervising/src/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright 2024, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use sel4::config::sel4_cfg_if;

sel4_cfg_if! {
if #[sel4_cfg(ARCH_AARCH64)] {
#[path = "aarch64.rs"]
mod imp;
} else if #[sel4_cfg(ARCH_AARCH32)] {
#[path = "aarch32.rs"]
mod imp;
} else if #[sel4_cfg(any(ARCH_RISCV64, ARCH_RISCV32))] {
#[path = "riscv.rs"]
mod imp;
} else if #[sel4_cfg(ARCH_X86_64)] {
#[path = "x86_64.rs"]
mod imp;
}
}

#[cfg(any())]
mod aarch32;
#[cfg(any())]
mod aarch64;
#[cfg(any())]
mod riscv;
#[cfg(any())]
mod x86_64;

pub use imp::*;
Loading

0 comments on commit 31065ed

Please sign in to comment.