Skip to content

Commit

Permalink
crates/sel4/bitfield-ops: Re-introduce Bitfield type
Browse files Browse the repository at this point in the history
  • Loading branch information
nspin committed Oct 4, 2023
1 parent 6174542 commit 0b118ba
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 61 deletions.
118 changes: 107 additions & 11 deletions crates/sel4/bitfield-ops/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]

use core::marker::PhantomData;
use core::mem;
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not, Range, Shl, Shr};

Expand Down Expand Up @@ -231,6 +232,101 @@ where

// // //

#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Bitfield<T, U> {
inner: T,
_phantom: PhantomData<U>,
}

impl<T, U> Bitfield<T, U> {
pub fn new(inner: T) -> Self {
Self {
inner,
_phantom: PhantomData,
}
}

pub fn into_inner(self) -> T {
self.inner
}

pub fn inner(&self) -> &T {
&self.inner
}

pub fn inner_mut(&mut self) -> &mut T {
&mut self.inner
}
}

impl<T: UnsignedPrimInt, const N: usize> Bitfield<[T; N], T> {
pub fn zeroed() -> Self {
Self::new([T::zero(); N])
}
}

impl<T: AsRef<[U]>, U: UnsignedPrimInt> Bitfield<T, U> {
pub fn bits(&self) -> &[U] {
self.inner().as_ref()
}

pub fn get_bits<V: UnsignedPrimInt + TryFrom<U>>(&self, range: Range<usize>) -> V {
get_bits(self.bits(), range)
}

pub fn get_bits_into_slice<V: UnsignedPrimInt>(
&self,
range: Range<usize>,
dst: &mut [V],
dst_start: usize,
) where
V: TryFrom<usize>,
usize: TryFrom<U>,
{
let dst_range = dst_start..(dst_start + range.len());
set_bits_from_slice(dst, dst_range, self.bits(), range.start)
}

pub fn get<V: PrimInt>(&self, start_bit: usize) -> V
where
V::Unsigned: TryFrom<U>,
{
get(self.bits(), start_bit)
}
}

impl<T: AsMut<[U]>, U: UnsignedPrimInt> Bitfield<T, U> {
pub fn bits_mut(&mut self) -> &mut [U] {
self.inner_mut().as_mut()
}

pub fn set_bits<V: UnsignedPrimInt + TryInto<U>>(&mut self, range: Range<usize>, src: V) {
set_bits(self.bits_mut(), range, src)
}

pub fn set_bits_from_slice<V: UnsignedPrimInt>(
&mut self,
range: Range<usize>,
src: &[V],
src_start: usize,
) where
U: TryFrom<usize>,
usize: TryFrom<V>,
{
set_bits_from_slice(self.bits_mut(), range, src, src_start)
}

pub fn set<V: PrimInt>(&mut self, start_bit: usize, src: V)
where
V::Unsigned: TryInto<U>,
{
set(self.bits_mut(), start_bit, src)
}
}

// // //

#[cfg(test)]
mod test {
#![allow(unused_imports)]
Expand All @@ -244,7 +340,7 @@ mod test {

#[test]
fn zero_gets_zero() {
assert_eq!(Bitfield::<u64, 2>::zeroed().get_bits(50..80), 0);
assert_eq!(Bitfield::<[u64; 2], _>::zeroed().get_bits::<u64>(50..80), 0);
}

fn set_and_get<
Expand All @@ -255,9 +351,9 @@ mod test {
range: Range<usize>,
val: U,
) {
let mut arr = Bitfield::<T, N>::zeroed();
set_bits(arr.as_mut_arr(), range.clone(), val);
let observed_val: U = get_bits(arr.as_arr(), range);
let mut arr = Bitfield::<[T; N], _>::zeroed();
set_bits(arr.inner_mut(), range.clone(), val);
let observed_val: U = get_bits(arr.inner(), range);
assert_eq!(observed_val, val);
}

Expand All @@ -271,13 +367,13 @@ mod test {
#[test]
fn this_works_too() {
for init in [0, !0] {
let mut arr = Bitfield::<u64, 1>::from_arr([init]);
arr.set_bits(0..2, 0b11);
arr.set_bits(60..64, 0b1111);
arr.set_bits(10..11, 0b1);
assert_eq!(arr.get_bits(0..2), 0b11);
assert_eq!(arr.get_bits(60..64), 0b1111);
assert_eq!(arr.get_bits(10..11), 0b1);
let mut arr = Bitfield::<[u64; 1], u64>::new([init]);
arr.set_bits::<u64>(0..2, 0b11);
arr.set_bits::<u64>(60..64, 0b1111);
arr.set_bits::<u64>(10..11, 0b1);
assert_eq!(arr.get_bits::<u64>(0..2), 0b11);
assert_eq!(arr.get_bits::<u64>(60..64), 0b1111);
assert_eq!(arr.get_bits::<u64>(10..11), 0b1);
}
}
}
2 changes: 1 addition & 1 deletion crates/sel4/src/cnode_cap_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl CNodeCapData {
}

pub fn into_word(self) -> Word {
let arr = self.inner().0.as_arr();
let arr = self.inner().0.inner();
assert_eq!(arr.len(), 1); // TODO assert at compile time instead
arr[0]
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sel4/sys/build/bf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl BackingType {
fn bitfield(&self) -> TokenStream {
let primitive = self.primitive();
let multiple = self.multiple;
quote!(Bitfield<#primitive, #multiple>)
quote!(SeL4Bitfield<#primitive, #multiple>)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sel4/sys/build/xml/invocations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<'a> InvocationGenerator<'a> {
});
}
ParameterType::Bitfield => toks.extend(quote! {
self.set_mr_bits(#start..#end, #name.0.as_arr()[0]);
self.set_mr_bits(#start..#end, #name.0.inner()[0]);
}),
ParameterType::Struct { members } => {
assert!(self.parameter_types.get(&param.ty).pass_by_reference());
Expand Down
4 changes: 2 additions & 2 deletions crates/sel4/sys/src/bf/mod.rs → crates/sel4/sys/src/bf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::fmt;

pub(crate) mod types;
use sel4_bitfield_ops::Bitfield;

use types::Bitfield;
pub(crate) type SeL4Bitfield<T, const N: usize> = Bitfield<[T; N], T>;

include!(concat!(env!("OUT_DIR"), "/types.rs"));
include!(concat!(env!("OUT_DIR"), "/shared_types.rs"));
42 changes: 0 additions & 42 deletions crates/sel4/sys/src/bf/types.rs

This file was deleted.

6 changes: 3 additions & 3 deletions crates/sel4/sys/src/syscalls/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bf::types::Bitfield;
use crate::bf::SeL4Bitfield;

use crate::{seL4_MessageInfo, seL4_Word};

Expand All @@ -8,11 +8,11 @@ pub use arch::*;

impl seL4_MessageInfo {
pub(crate) fn from_word(word: seL4_Word) -> Self {
Self(Bitfield::from_arr([word]))
Self(SeL4Bitfield::new([word]))
}

pub(crate) fn into_word(self) -> seL4_Word {
self.0.into_arr()[0]
self.0.into_inner()[0]
}

pub(crate) fn msg_helper(&self, msg: Option<seL4_Word>, i: seL4_Word) -> seL4_Word {
Expand Down

0 comments on commit 0b118ba

Please sign in to comment.