feat: add utils/bin

This commit is contained in:
Paul Pan 2024-04-20 18:17:56 +08:00
parent ea80bd3e8d
commit c371fd543f
3 changed files with 11 additions and 3 deletions

View File

@ -1,5 +1,6 @@
use num_traits::ToPrimitive;
use utils::size::{GIB, KIB, MIB, TIB};
use utils::MASK;
use vspace::addr::{AddressOps, PhysAddr, VirtAddr};
use vspace::paging::TableLevel;
@ -8,8 +9,8 @@ pub trait PhysAddrPaging {
const PPN_BITS: usize;
const PPN_OFFSET: usize;
const PA_PPN_MASK: usize = ((1 << Self::PPN_BITS) - 1) << Self::PG_OFFSET;
const PTE_PPN_MASK: usize = ((1 << Self::PPN_BITS) - 1) << Self::PPN_OFFSET;
const PA_PPN_MASK: usize = MASK!(Self::PPN_BITS) << Self::PG_OFFSET;
const PTE_PPN_MASK: usize = MASK!(Self::PPN_BITS) << Self::PPN_OFFSET;
fn to_ppn(&self) -> usize
where Self: AddressOps {
@ -61,7 +62,7 @@ pub trait VirtAddrPaging {
fn lower_bits(&self, level: usize) -> usize
where Self: AddressOps {
self.as_usize() & ((1 << (Self::PG_OFFSET + Self::VPN_BITS * (level + 1))) - 1)
self.as_usize() & MASK!(Self::PG_OFFSET + Self::VPN_BITS * (level + 1))
}
}

6
lib/utils/src/bin.rs Normal file
View File

@ -0,0 +1,6 @@
#[macro_export]
macro_rules! MASK {
($bits:expr) => {
(1 << $bits) - 1
};
}

View File

@ -3,6 +3,7 @@
pub mod assert;
pub mod atomic;
pub mod bin;
pub mod container_of;
pub mod extern_addr;
pub mod function_name;