Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further reduce reliance on unstable rustc features #62

Merged
merged 15 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/examples/microkit/hello/pds/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#![no_std]
#![no_main]
#![feature(never_type)]

use sel4_microkit::{debug_println, protection_domain, Handler};

Expand All @@ -17,4 +18,6 @@ fn init() -> HandlerImpl {

struct HandlerImpl;

impl Handler for HandlerImpl {}
impl Handler for HandlerImpl {
type Error = !;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

#![no_std]
#![feature(slice_ptr_get)]

use core::alloc::Layout;
use core::ptr::{self, NonNull};
Expand Down Expand Up @@ -81,7 +80,7 @@ unsafe impl Hal for HalImpl {
.as_mut_ptr()
.index(bounce_buffer_range.clone());
bounce_buffer_ptr.fill(0);
let vaddr = bounce_buffer_ptr.as_raw_ptr().as_non_null_ptr();
let vaddr = bounce_buffer_ptr.as_raw_ptr().cast::<u8>();
let paddr = state.offset_to_paddr(bounce_buffer_range.start);
(paddr, vaddr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// SPDX-License-Identifier: BSD-2-Clause
//

use sel4_dlmalloc::StaticDlmallocGlobalAlloc;
use sel4_dlmalloc::{StaticDlmallocGlobalAlloc, StaticHeapBounds};
use sel4_sync::AbstractMutexSyncOps;

use crate::{get_static_heap_bounds, get_static_heap_mutex_notification};
Expand All @@ -13,7 +13,7 @@ use crate::{get_static_heap_bounds, get_static_heap_mutex_notification};
#[allow(clippy::type_complexity)]
static GLOBAL_ALLOCATOR: StaticDlmallocGlobalAlloc<
AbstractMutexSyncOps<fn(), fn()>,
fn() -> *mut [u8],
fn() -> StaticHeapBounds,
> = StaticDlmallocGlobalAlloc::new(
AbstractMutexSyncOps {
signal: || {
Expand Down
6 changes: 3 additions & 3 deletions crates/private/support/sel4-simple-task/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
extern crate alloc;

use core::ffi::{c_char, c_void};
use core::ptr;
use core::slice;

use sel4::Endpoint;
use sel4_backtrace_simple::SimpleBacktracing;
use sel4_dlmalloc::StaticHeapBounds;
use sel4_immediate_sync_once_cell::ImmediateSyncOnceCell;
use sel4_panicking::ExternalPanicInfo;
use sel4_panicking_env::{abort, AbortInfo};
Expand Down Expand Up @@ -134,9 +134,9 @@ fn panic_hook(info: &ExternalPanicInfo<'_>) {
get_backtracing().collect_and_send();
}

fn get_static_heap_bounds() -> *mut [u8] {
fn get_static_heap_bounds() -> StaticHeapBounds {
let addrs = CONFIG.get().unwrap().static_heap().unwrap();
ptr::slice_from_raw_parts_mut(
StaticHeapBounds::new(
usize::try_from(addrs.start).unwrap() as *mut _,
(addrs.end - addrs.start).try_into().unwrap(),
)
Expand Down
21 changes: 15 additions & 6 deletions crates/sel4-async/block-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

#![no_std]
#![feature(associated_type_bounds)]
#![feature(never_type)]

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -214,7 +213,10 @@ impl<T> NextBlockSizeAdapter<T> {
wrapper_methods!(T);
}

impl<T: BlockIOLayout<BlockSize: HasNextBlockSize>> BlockIOLayout for NextBlockSizeAdapter<T> {
impl<T: BlockIOLayout> BlockIOLayout for NextBlockSizeAdapter<T>
where
T::BlockSize: HasNextBlockSize,
{
type Error = T::Error;

type BlockSize = <T::BlockSize as HasNextBlockSize>::NextBlockSize;
Expand All @@ -230,7 +232,10 @@ impl<T: BlockIOLayout<BlockSize: HasNextBlockSize>> BlockIOLayout for NextBlockS
}
}

impl<T: BlockIO<A, BlockSize: HasNextBlockSize>, A: Access> BlockIO<A> for NextBlockSizeAdapter<T> {
impl<T: BlockIO<A>, A: Access> BlockIO<A> for NextBlockSizeAdapter<T>
where
T::BlockSize: HasNextBlockSize,
{
async fn read_or_write_blocks(
&self,
start_block_idx: u64,
Expand All @@ -256,7 +261,10 @@ impl<T> PrevBlockSizeAdapter<T> {
wrapper_methods!(T);
}

impl<T: BlockIOLayout<BlockSize: HasPrevBlockSize>> BlockIOLayout for PrevBlockSizeAdapter<T> {
impl<T: BlockIOLayout> BlockIOLayout for PrevBlockSizeAdapter<T>
where
T::BlockSize: HasPrevBlockSize,
{
type Error = T::Error;

type BlockSize = <T::BlockSize as HasPrevBlockSize>::PrevBlockSize;
Expand All @@ -270,8 +278,9 @@ impl<T: BlockIOLayout<BlockSize: HasPrevBlockSize>> BlockIOLayout for PrevBlockS
}
}

impl<T: BlockIO<A, BlockSize: HasPrevBlockSize>, A: ReadAccess> BlockIO<A>
for PrevBlockSizeAdapter<T>
impl<T: BlockIO<A>, A: ReadAccess> BlockIO<A> for PrevBlockSizeAdapter<T>
where
T::BlockSize: HasPrevBlockSize,
{
async fn read_or_write_blocks(
&self,
Expand Down
7 changes: 0 additions & 7 deletions crates/sel4-async/time/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,5 @@ mk {
package.name = "sel4-async-time";
dependencies = {
inherit (versions) log pin-project;
# futures = {
# version = versions.futures;
# default-features = false;
# features = [
# "alloc"
# ];
# };
};
}
1 change: 0 additions & 1 deletion crates/sel4-async/time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

#![no_std]
#![feature(btree_cursors)]

extern crate alloc;

Expand Down
105 changes: 70 additions & 35 deletions crates/sel4-async/time/src/timer_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
// SPDX-License-Identifier: BSD-2-Clause
//

use alloc::collections::btree_map::{BTreeMap, CursorMut};
use core::ops::Bound;
use alloc::collections::btree_map::{BTreeMap, Entry};

use crate::SubKey;

Expand All @@ -16,8 +15,14 @@ use crate::SubKey;

// TODO: Add feature like `tokio::time::Interval`

// NOTE(rustc_wishlist)
//
// Once #![feature(btree_cursors)] stabilizes, revert back to using it for a simpler, more
// lightweight, and more efficient (on the small scale) implementation. See git history for such an
// implementation.

pub struct TimerQueue<T, U, V> {
pending: BTreeMap<Key<T, U>, V>,
pending: BTreeMap<T, BTreeMap<U, V>>,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -40,46 +45,65 @@ impl<T: Ord + Clone, U: SubKey + Clone, V> TimerQueue<T, U, V> {
}

pub fn peek_next_absolute_expiry(&self) -> Option<&T> {
let (key, _value) = self.pending.first_key_value()?;
Some(key.absolute_expiry())
let (absolute_expiry, _value) = self.pending.first_key_value()?;
Some(absolute_expiry)
}

pub fn insert(&mut self, absolute_expiry: T, value: V) -> Key<T, U> {
let upper_bound = Key {
absolute_expiry: absolute_expiry.clone(),
sub_key: SubKey::max(),
};
let mut cursor = self.pending.upper_bound_mut(Bound::Included(&upper_bound));
let sub_key = match cursor.key() {
None => SubKey::min(),
Some(prev_key) if prev_key.absolute_expiry() == &absolute_expiry => prev_key
.sub_key
.succ()
.expect("too many timers for one instant"),
Some(_) => SubKey::max(),
let sub_key = match self.pending.entry(absolute_expiry.clone()) {
Entry::Vacant(entry) => {
let sub_key = <U as SubKey>::min();
entry.insert(BTreeMap::new()).insert(sub_key.clone(), value);
sub_key
}
Entry::Occupied(mut entry) => {
let sub_map = entry.get_mut();
let sub_key = sub_map
.last_entry()
.unwrap()
.key()
.succ()
.expect("too many timers for one instant");
sub_map.insert(sub_key.clone(), value);
sub_key
}
};
let key = Key {

Key {
absolute_expiry,
sub_key,
};
cursor.insert_after(key.clone(), value);
key
}
}

pub fn try_remove(&mut self, key: &Key<T, U>) -> Option<Expired<T, U, V>> {
let (key, value) = self.pending.remove_entry(key)?;
Some(Expired { key, value })
match self
.pending
.entry(key.absolute_expiry().clone() /* HACK silly clone */)
{
Entry::Vacant(_) => None,
Entry::Occupied(mut entry) => {
let sub_map = entry.get_mut();
let (sub_key, value) = sub_map.remove_entry(&key.sub_key)?;
if sub_map.is_empty() {
entry.remove();
}
Some(Expired {
key: Key {
absolute_expiry: key.absolute_expiry().clone(),
sub_key,
},
value,
})
}
}
}

pub fn remove(&mut self, key: &Key<T, U>) -> Expired<T, U, V> {
self.try_remove(key).unwrap()
}

pub fn iter_expired(&mut self, now: T) -> IterExpired<'_, T, U, V> {
IterExpired {
now,
cursor: self.pending.lower_bound_mut(Bound::Unbounded),
}
IterExpired { now, queue: self }
}
}

Expand Down Expand Up @@ -107,19 +131,30 @@ impl<T, U, V> Expired<T, U, V> {

pub struct IterExpired<'a, T, U, V> {
now: T,
cursor: CursorMut<'a, Key<T, U>, V>,
queue: &'a mut TimerQueue<T, U, V>,
}

impl<'a, T: Ord, U: Ord, V> Iterator for IterExpired<'a, T, U, V> {
impl<'a, T: Ord + Clone, U: Ord, V> Iterator for IterExpired<'a, T, U, V> {
type Item = Expired<T, U, V>;

fn next(&mut self) -> Option<Self::Item> {
let key = self.cursor.key()?;
if key.absolute_expiry() <= &self.now {
let (key, value) = self.cursor.remove_current().unwrap();
Some(Expired { key, value })
} else {
None
let mut entry = self.queue.pending.first_entry()?;
if entry.key() > &self.now {
return None;
}
let sub_map = entry.get_mut();
let (sub_key, value) = sub_map.pop_first().unwrap();
let absolute_expiry = if sub_map.is_empty() {
entry.remove_entry().0
} else {
entry.key().clone()
};
Some(Expired {
key: Key {
absolute_expiry,
sub_key,
},
value,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// SPDX-License-Identifier: BSD-2-Clause
//

#![feature(associated_type_bounds)]

use std::fs;
use std::io;

Expand Down Expand Up @@ -63,10 +61,12 @@ fn main() -> Result<(), io::Error> {
fs::write(out_elf_path, out_elf)
}

fn with_bit_width<T: FileHeaderExt<Word: PrimInt, Sword: PrimInt>>(
image_elf: &[u8],
content: &[u8],
) -> Vec<u8> {
fn with_bit_width<T>(image_elf: &[u8], content: &[u8]) -> Vec<u8>
where
T: FileHeaderExt,
T::Word: PrimInt,
T::Sword: PrimInt,
{
let content_len = NumCast::from(content.len()).unwrap();
let mut input = Input::<T>::default();
input.symbolic_injections.push(SymbolicInjection {
Expand Down
Loading