diff --git a/Cargo.lock b/Cargo.lock index 1812e83..7bdc594 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 4 [[package]] name = "libsys" -version = "0.2.1" +version = "0.3.0" dependencies = [ "num_enum", "spin", diff --git a/Cargo.toml b/Cargo.toml index 59b5781..8d347d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libsys" -version = "0.2.1" +version = "0.3.0" edition = "2024" [dependencies] diff --git a/src/address/frame.rs b/src/address/frame.rs index c60c4e9..886f9c3 100644 --- a/src/address/frame.rs +++ b/src/address/frame.rs @@ -1,6 +1,6 @@ use crate::{ address::{Address, AddressKind, NonCanonicalError, Physical}, - constants::{is_physical_address_canonical, page_mask, page_shift, truncate_physical_address}, + constants::{is_physical_address_canonical, page_bits, page_mask, truncate_physical_address}, }; pub struct Frame; @@ -78,7 +78,7 @@ impl Address { /// /// - [`NonCanonicalError`] if `index` would create a non-canonical address. pub fn from_index(index: usize) -> Result { - let physical_address = index << page_shift().get(); + let physical_address = index << page_bits().get(); if is_physical_address_canonical(physical_address) { Ok(Self(physical_address)) @@ -90,7 +90,7 @@ impl Address { /// Gets the index of the frame this address points to. #[must_use] pub fn index(&self) -> usize { - self.0 >> page_shift().get() + self.0 >> page_bits().get() } } diff --git a/src/address/page.rs b/src/address/page.rs index 08d0ef5..a8a0bce 100644 --- a/src/address/page.rs +++ b/src/address/page.rs @@ -1,7 +1,7 @@ use crate::{ address::{Address, AddressKind, NonCanonicalError, Virtual}, constants::{ - is_virtual_address_canonical, page_mask, page_shift, page_size, truncate_virtual_address, + is_virtual_address_canonical, page_bits, page_mask, page_size, truncate_virtual_address, }, }; @@ -80,7 +80,7 @@ impl Address { /// /// - [`NonCanonicalError`] if `index` would create a non-canonical address. pub fn from_index(index: usize) -> Result { - let virtual_address = index << page_shift().get(); + let virtual_address = index << page_bits().get(); if is_virtual_address_canonical(virtual_address) { Ok(Self(virtual_address)) @@ -92,7 +92,7 @@ impl Address { /// Gets the index of the page this address points to. #[must_use] pub fn index(&self) -> usize { - self.0 >> page_shift().get() + self.0 >> page_bits().get() } } diff --git a/src/constants/x86_64.rs b/src/constants/x86_64.rs index ca59370..0dfc421 100644 --- a/src/constants/x86_64.rs +++ b/src/constants/x86_64.rs @@ -3,40 +3,40 @@ use core::num::NonZero; /// Bit shift required to offset page indexes. #[must_use] -pub const fn page_shift() -> NonZero { +pub const fn page_bits() -> NonZero { NonZero::::new(12).unwrap() } -/// Bit shift required to offset mega page indexes. +/// Bit shift required to offset large page indexes. #[must_use] -pub const fn mega_page_shift() -> NonZero { - page_shift().checked_add(table_index_shift().get()).unwrap() +pub const fn large_page_bits() -> NonZero { + page_bits().checked_add(table_index_bits().get()).unwrap() } -/// Bit shift required to offset giga page indexes. +/// Bit shift required to offset huge page indexes. #[must_use] -pub const fn giga_page_shift() -> NonZero { - mega_page_shift() - .checked_add(table_index_shift().get()) +pub const fn huge_page_bits() -> NonZero { + large_page_bits() + .checked_add(table_index_bits().get()) .unwrap() } /// The size of a page in bytes. #[must_use] pub const fn page_size() -> usize { - 1usize.checked_shl(page_shift().get()).unwrap() + 1usize.checked_shl(page_bits().get()).unwrap() } -/// The size of a mega page in bytes. +/// The size of a large page in bytes. #[must_use] -pub const fn mega_page_size() -> usize { - 1usize.checked_shl(mega_page_shift().get()).unwrap() +pub const fn large_page_size() -> usize { + 1usize.checked_shl(large_page_bits().get()).unwrap() } -/// The size of a giga page in bytes. +/// The size of a huge page in bytes. #[must_use] -pub const fn giga_page_size() -> usize { - 1usize.checked_shl(giga_page_shift().get()).unwrap() +pub const fn huge_page_size() -> usize { + 1usize.checked_shl(huge_page_bits().get()).unwrap() } /// Bit-mask of non-index page bytes. @@ -45,28 +45,28 @@ pub const fn page_mask() -> usize { page_size().checked_sub(1).unwrap() } -/// Bit-mask of non-index mega page bytes. +/// Bit-mask of non-index large page bytes. #[must_use] -pub const fn mega_page_mask() -> usize { - mega_page_size().checked_sub(1).unwrap() +pub const fn large_page_mask() -> usize { + large_page_size().checked_sub(1).unwrap() } -/// Bit-mask of non-index giga page bytes. +/// Bit-mask of non-index huge page bytes. #[must_use] -pub const fn giga_page_mask() -> usize { - giga_page_size().checked_sub(1).unwrap() +pub const fn huge_page_mask() -> usize { + huge_page_size().checked_sub(1).unwrap() } /// Shift (in bits) of a page table index. #[must_use] -pub const fn table_index_shift() -> NonZero { +pub const fn table_index_bits() -> NonZero { NonZero::::new(9).unwrap() } /// Size (in bytes) of a page table index. #[must_use] pub const fn table_index_size() -> usize { - 1 << table_index_shift().get() + 1 << table_index_bits().get() } /// Bit-mask of a page table index. @@ -75,11 +75,24 @@ pub const fn table_index_mask() -> usize { table_index_size().checked_sub(1).unwrap() } +/// Number of bits in a canonical physical address. +#[must_use] +pub const fn physical_address_bits() -> NonZero { + NonZero::::new(52).unwrap() +} + +/// The maximum physical address size (in bytes). +#[must_use] +pub const fn physical_address_size() -> usize { + 1usize.checked_shl(physical_address_bits().get()).unwrap() +} + /// Bit-mask of canonical physical bits. #[must_use] pub const fn physical_address_mask() -> usize { - 0x000F_FFFF_FFFF_FFFF + physical_address_size().checked_sub(1).unwrap() } + /// Checks if the provided `physical_address` is canonical. #[must_use] pub const fn is_physical_address_canonical(physical_address: usize) -> bool { @@ -88,9 +101,9 @@ pub const fn is_physical_address_canonical(physical_address: usize) -> bool { /// Bit-shift to reach non-canonical bits of a virtual address. #[must_use] -pub fn virtual_address_noncanonical_shift() -> NonZero { - let table_indexes_shift = table_index_shift().checked_mul(get_paging_depth()).unwrap(); - let total_shift = table_indexes_shift.checked_add(page_shift().get()).unwrap(); +pub fn virtual_address_bits() -> NonZero { + let table_indexes_shift = table_index_bits().checked_mul(get_paging_depth()).unwrap(); + let total_shift = table_indexes_shift.checked_add(page_bits().get()).unwrap(); NonZero::::new(total_shift.get()).unwrap() } @@ -98,10 +111,7 @@ pub fn virtual_address_noncanonical_shift() -> NonZero { /// Checks whether a provided address has only the canonical virtual bits. #[must_use] pub fn is_virtual_address_canonical(virtual_address: usize) -> bool { - let sign_extension_check_shift = virtual_address_noncanonical_shift() - .get() - .checked_sub(1) - .unwrap(); + let sign_extension_check_shift = virtual_address_bits().get().checked_sub(1).unwrap(); matches!(virtual_address >> sign_extension_check_shift, 0 | 0x1FFFF) } @@ -114,6 +124,6 @@ pub fn truncate_physical_address(address: usize) -> usize { #[must_use] #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)] pub fn truncate_virtual_address(address: usize) -> usize { - let sign_extension_shift = usize::BITS - virtual_address_noncanonical_shift().get(); + let sign_extension_shift = usize::BITS - virtual_address_bits().get(); (((address << sign_extension_shift) as isize) >> sign_extension_shift) as usize }