style: cargo fmt kernel crates

This commit is contained in:
2026-10-26 19:07:54 +01:00
parent 818eaecadd
commit 1c04ddf909
50 changed files with 995 additions and 499 deletions

View File

@@ -17,11 +17,7 @@ pub struct Capability {
}
impl Capability {
pub fn new(
resource: Resource,
permissions: PermissionSet,
generation: Generation,
) -> Self {
pub fn new(resource: Resource, permissions: PermissionSet, generation: Generation) -> Self {
Self {
resource,
permissions,

View File

@@ -95,15 +95,21 @@ impl EndpointTable {
}
pub fn get(&self, id: EndpointId) -> Option<&Endpoint> {
self.endpoints.get(id.raw() as usize).and_then(|s| s.as_ref())
self.endpoints
.get(id.raw() as usize)
.and_then(|s| s.as_ref())
}
pub fn get_mut(&mut self, id: EndpointId) -> Option<&mut Endpoint> {
self.endpoints.get_mut(id.raw() as usize).and_then(|s| s.as_mut())
self.endpoints
.get_mut(id.raw() as usize)
.and_then(|s| s.as_mut())
}
pub fn destroy(&mut self, id: EndpointId) -> Option<Endpoint> {
self.endpoints.get_mut(id.raw() as usize).and_then(|s| s.take())
self.endpoints
.get_mut(id.raw() as usize)
.and_then(|s| s.take())
}
pub fn remove_thread_from_all(&mut self, thread: ThreadId) {

View File

@@ -166,7 +166,11 @@ fn table_destroy_then_get_fails() {
fn table_get_mut_allows_enqueue() {
let mut table = EndpointTable::new();
let id = table.create().unwrap();
table.get_mut(id).unwrap().enqueue_sender(thread(1)).unwrap();
table
.get_mut(id)
.unwrap()
.enqueue_sender(thread(1))
.unwrap();
assert!(table.get(id).unwrap().has_waiting_sender());
}

View File

@@ -87,11 +87,15 @@ impl RegionTable {
}
pub fn get_mut(&mut self, id: RegionId) -> Option<&mut MemoryRegion> {
self.regions.get_mut(id.raw() as usize).and_then(|s| s.as_mut())
self.regions
.get_mut(id.raw() as usize)
.and_then(|s| s.as_mut())
}
pub fn remove(&mut self, id: RegionId) -> Option<MemoryRegion> {
self.regions.get_mut(id.raw() as usize).and_then(|s| s.take())
self.regions
.get_mut(id.raw() as usize)
.and_then(|s| s.take())
}
pub fn find_by_base(

View File

@@ -178,13 +178,15 @@ fn region_table_insert_and_get() {
#[test]
fn region_table_remove() {
let mut table = RegionTable::new();
let id = table.insert(MemoryRegion::new(
RegionId::from_raw(0),
process(0),
base(),
pages(4),
Generation::initial(),
)).unwrap();
let id = table
.insert(MemoryRegion::new(
RegionId::from_raw(0),
process(0),
base(),
pages(4),
Generation::initial(),
))
.unwrap();
let removed = table.remove(id);
assert!(removed.is_some());
assert!(table.get(id).is_none());
@@ -194,13 +196,25 @@ fn region_table_remove() {
fn region_table_regions_for_process() {
let mut table = RegionTable::new();
table.insert(MemoryRegion::new(
RegionId::from_raw(0), process(0), base(), pages(4), Generation::initial(),
RegionId::from_raw(0),
process(0),
base(),
pages(4),
Generation::initial(),
));
table.insert(MemoryRegion::new(
RegionId::from_raw(1), process(1), base(), pages(4), Generation::initial(),
RegionId::from_raw(1),
process(1),
base(),
pages(4),
Generation::initial(),
));
table.insert(MemoryRegion::new(
RegionId::from_raw(2), process(0), base(), pages(2), Generation::initial(),
RegionId::from_raw(2),
process(0),
base(),
pages(2),
Generation::initial(),
));
assert_eq!(table.regions_for_process(process(0)).count(), 2);
assert_eq!(table.regions_for_process(process(1)).count(), 1);

View File

@@ -44,10 +44,7 @@ fn caps_returns_full_array() {
#[test]
fn builder_sets_individual_words() {
let msg = Message::builder()
.word(0, 42)
.word(2, 99)
.build();
let msg = Message::builder().word(0, 42).word(2, 99).build();
assert_eq!(msg.word(0), 42);
assert_eq!(msg.word(1), 0);
assert_eq!(msg.word(2), 99);
@@ -56,10 +53,7 @@ fn builder_sets_individual_words() {
#[test]
fn builder_sets_caps() {
let msg = Message::builder()
.cap(0, slot(5))
.cap(1, slot(10))
.build();
let msg = Message::builder().cap(0, slot(5)).cap(1, slot(10)).build();
assert_eq!(msg.cap(0), Some(slot(5)));
assert_eq!(msg.cap(1), Some(slot(10)));
assert!(msg.cap(2).is_none());
@@ -79,8 +73,6 @@ fn builder_sets_words_and_caps_together() {
#[test]
fn builder_with_all_words() {
let msg = Message::builder()
.words([11, 22, 33, 44])
.build();
let msg = Message::builder().words([11, 22, 33, 44]).build();
assert_eq!(msg.words(), &[11, 22, 33, 44]);
}

View File

@@ -1,6 +1,6 @@
use super::{arbitrary_endpoint_cap, insert_cap};
use crate::capability::CapabilityTable;
use crate::resource::Resource;
use super::{arbitrary_endpoint_cap, insert_cap};
use types::permission::PermissionSet;
#[kani::proof]

View File

@@ -1,6 +1,6 @@
use super::{arbitrary_endpoint_cap, insert_cap};
use crate::capability::{Capability, CapabilityTable};
use crate::resource::Resource;
use super::{arbitrary_endpoint_cap, insert_cap};
use types::generation::Generation;
use types::id::EndpointId;
use types::permission::{Permission, PermissionSet};
@@ -16,7 +16,10 @@ fn delegation_via_cap_table_never_escalates() {
kani::assume(source_perms.allows(Permission::Delegate));
let mut source_table = CapabilityTable::new();
let slot = insert_cap(&mut source_table, arbitrary_endpoint_cap(source_bits, ep_id));
let slot = insert_cap(
&mut source_table,
arbitrary_endpoint_cap(source_bits, ep_id),
);
let cap = source_table.get(slot).unwrap();
let original_perms = *cap.permissions();
@@ -43,11 +46,21 @@ fn delegation_cannot_add_permissions_not_in_source() {
let source = PermissionSet::from_bits(source_bits);
let result = source.restrict_to(&PermissionSet::from_bits(restrict_bits));
if !source.allows(Permission::Read) { assert!(!result.allows(Permission::Read)); }
if !source.allows(Permission::Write) { assert!(!result.allows(Permission::Write)); }
if !source.allows(Permission::Execute) { assert!(!result.allows(Permission::Execute)); }
if !source.allows(Permission::Delegate) { assert!(!result.allows(Permission::Delegate)); }
if !source.allows(Permission::Revoke) { assert!(!result.allows(Permission::Revoke)); }
if !source.allows(Permission::Read) {
assert!(!result.allows(Permission::Read));
}
if !source.allows(Permission::Write) {
assert!(!result.allows(Permission::Write));
}
if !source.allows(Permission::Execute) {
assert!(!result.allows(Permission::Execute));
}
if !source.allows(Permission::Delegate) {
assert!(!result.allows(Permission::Delegate));
}
if !source.allows(Permission::Revoke) {
assert!(!result.allows(Permission::Revoke));
}
}
#[kani::proof]

View File

@@ -3,7 +3,7 @@ use types::{
id::{CoreId, ThreadId},
};
use super::{per_core::PerCoreScheduler, Scheduler, SchedulerDecision};
use super::{Scheduler, SchedulerDecision, per_core::PerCoreScheduler};
const MAX_CORES: usize = 8;
const MAX_SCHEDULED_THREADS: usize = 256;

View File

@@ -12,11 +12,11 @@ pub struct WaitSet {
}
impl WaitSet {
pub fn new(
endpoints: [Option<CapSlot>; 4],
interrupts: [Option<CapSlot>; 4],
) -> Self {
Self { endpoints, interrupts }
pub fn new(endpoints: [Option<CapSlot>; 4], interrupts: [Option<CapSlot>; 4]) -> Self {
Self {
endpoints,
interrupts,
}
}
pub fn endpoints(&self) -> &[Option<CapSlot>; 4] {
@@ -28,8 +28,7 @@ impl WaitSet {
}
pub fn is_empty(&self) -> bool {
self.endpoints.iter().all(|s| s.is_none())
&& self.interrupts.iter().all(|s| s.is_none())
self.endpoints.iter().all(|s| s.is_none()) && self.interrupts.iter().all(|s| s.is_none())
}
}
@@ -88,7 +87,9 @@ pub enum Syscall {
Exit,
Halt,
CreateEndpoint,
DebugPutchar { byte: u8 },
DebugPutchar {
byte: u8,
},
MapPage {
process: CapSlot,
src_va: VirtualAddress,

View File

@@ -236,7 +236,13 @@ impl ThreadTable {
.ok_or(KernelError::TooManyThreads)?;
let id = ThreadId::from_raw(idx as u16);
self.threads[idx] = Some(Thread::new(id, process, priority, time_slice, core_affinity));
self.threads[idx] = Some(Thread::new(
id,
process,
priority,
time_slice,
core_affinity,
));
Ok(id)
}
@@ -255,7 +261,14 @@ impl ThreadTable {
.ok_or(KernelError::TooManyThreads)?;
let id = ThreadId::from_raw(idx as u16);
self.threads[idx] = Some(Thread::with_context(id, process, priority, time_slice, core_affinity, context));
self.threads[idx] = Some(Thread::with_context(
id,
process,
priority,
time_slice,
core_affinity,
context,
));
Ok(id)
}

View File

@@ -66,7 +66,10 @@ fn running_to_blocked() {
let mut t = test_thread(0, 0);
t.set_running(core(0)).unwrap();
t.block(BlockReason::WaitingAny).unwrap();
assert!(matches!(t.state(), ThreadState::Blocked(BlockReason::WaitingAny)));
assert!(matches!(
t.state(),
ThreadState::Blocked(BlockReason::WaitingAny)
));
}
#[test]
@@ -162,7 +165,9 @@ fn time_slice_mut_ticks() {
#[test]
fn table_spawn_and_lookup() {
let mut table = ThreadTable::new();
let id = table.spawn(process(0), prio(10), time_slice(), None).unwrap();
let id = table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
let t = table.get(id).unwrap();
assert_eq!(t.id(), id);
assert_eq!(t.process(), process(0));
@@ -171,15 +176,21 @@ fn table_spawn_and_lookup() {
#[test]
fn table_spawn_returns_unique_ids() {
let mut table = ThreadTable::new();
let a = table.spawn(process(0), prio(10), time_slice(), None).unwrap();
let b = table.spawn(process(0), prio(10), time_slice(), None).unwrap();
let a = table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
let b = table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
assert_ne!(a, b);
}
#[test]
fn table_destroy_then_get_fails() {
let mut table = ThreadTable::new();
let id = table.spawn(process(0), prio(10), time_slice(), None).unwrap();
let id = table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
table.destroy(id).unwrap();
assert!(table.get(id).is_err());
}
@@ -187,9 +198,15 @@ fn table_destroy_then_get_fails() {
#[test]
fn table_threads_for_process_filters() {
let mut table = ThreadTable::new();
table.spawn(process(0), prio(10), time_slice(), None).unwrap();
table.spawn(process(1), prio(10), time_slice(), None).unwrap();
table.spawn(process(0), prio(5), time_slice(), None).unwrap();
table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
table
.spawn(process(1), prio(10), time_slice(), None)
.unwrap();
table
.spawn(process(0), prio(5), time_slice(), None)
.unwrap();
let count = table.threads_for_process(process(0)).count();
assert_eq!(count, 2);
@@ -198,16 +215,25 @@ fn table_threads_for_process_filters() {
#[test]
fn table_get_mut_allows_state_transition() {
let mut table = ThreadTable::new();
let id = table.spawn(process(0), prio(10), time_slice(), None).unwrap();
let id = table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
table.get_mut(id).unwrap().set_running(core(0)).unwrap();
assert!(matches!(table.get(id).unwrap().state(), ThreadState::Running(_)));
assert!(matches!(
table.get(id).unwrap().state(),
ThreadState::Running(_)
));
}
#[test]
fn table_destroy_reuses_slot() {
let mut table = ThreadTable::new();
let id = table.spawn(process(0), prio(10), time_slice(), None).unwrap();
let id = table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
table.destroy(id).unwrap();
let new_id = table.spawn(process(0), prio(10), time_slice(), None).unwrap();
let new_id = table
.spawn(process(0), prio(10), time_slice(), None)
.unwrap();
assert_eq!(id, new_id);
}