From 4e71da5cc55dbaf9e2a61b4a5665567006e9ea76 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 25 Sep 2019 13:49:02 +0200 Subject: [PATCH] Replace `ux` dependency with custom wrapper structs --- Cargo.toml | 4 -- src/addr.rs | 22 +++---- src/lib.rs | 5 -- .../paging/mapper/recursive_page_table.rs | 18 ++--- src/structures/paging/mod.rs | 2 +- src/structures/paging/page.rs | 28 +++++--- src/structures/paging/page_table.rs | 65 +++++++++++++++++-- 7 files changed, 101 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cb22118c7..3a09f0764 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,9 +34,5 @@ array-init = "0.0.4" version = "0.2.2" default-features = false -[dependencies.ux] -default-features = false -version = "0.1.3" - [features] deny-warnings = [] diff --git a/src/addr.rs b/src/addr.rs index 9d963789d..d4c1d5954 100644 --- a/src/addr.rs +++ b/src/addr.rs @@ -2,8 +2,8 @@ use core::convert::{Into, TryInto}; use core::fmt; use core::ops::{Add, AddAssign, Sub, SubAssign}; +use crate::structures::paging::{PageOffset, PageTableIndex}; use bit_field::BitField; -use ux::*; /// A canonical 64-bit virtual memory address. /// @@ -135,28 +135,28 @@ impl VirtAddr { } /// Returns the 12-bit page offset of this virtual address. - pub fn page_offset(&self) -> u12 { - u12::new((self.0 & 0xfff).try_into().unwrap()) + pub fn page_offset(&self) -> PageOffset { + PageOffset::new((self.0 & 0xfff).try_into().unwrap()) } /// Returns the 9-bit level 1 page table index. - pub fn p1_index(&self) -> u9 { - u9::new(((self.0 >> 12) & 0o777).try_into().unwrap()) + pub fn p1_index(&self) -> PageTableIndex { + PageTableIndex::new(((self.0 >> 12) & 0o777).try_into().unwrap()) } /// Returns the 9-bit level 2 page table index. - pub fn p2_index(&self) -> u9 { - u9::new(((self.0 >> 12 >> 9) & 0o777).try_into().unwrap()) + pub fn p2_index(&self) -> PageTableIndex { + PageTableIndex::new(((self.0 >> 12 >> 9) & 0o777).try_into().unwrap()) } /// Returns the 9-bit level 3 page table index. - pub fn p3_index(&self) -> u9 { - u9::new(((self.0 >> 12 >> 9 >> 9) & 0o777).try_into().unwrap()) + pub fn p3_index(&self) -> PageTableIndex { + PageTableIndex::new(((self.0 >> 12 >> 9 >> 9) & 0o777).try_into().unwrap()) } /// Returns the 9-bit level 4 page table index. - pub fn p4_index(&self) -> u9 { - u9::new(((self.0 >> 12 >> 9 >> 9 >> 9) & 0o777).try_into().unwrap()) + pub fn p4_index(&self) -> PageTableIndex { + PageTableIndex::new(((self.0 >> 12 >> 9 >> 9 >> 9) & 0o777).try_into().unwrap()) } } diff --git a/src/lib.rs b/src/lib.rs index a2dda0821..1407ff8fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,11 +10,6 @@ #![cfg_attr(not(feature = "deny-warnings"), warn(missing_docs))] #![deny(missing_debug_implementations)] -/// Provides the non-standard-width integer types `u2`–`u63`. -/// -/// We use these integer types in various APIs, for example `u9` for page tables indices. -pub use ux; - pub use crate::addr::{align_down, align_up, PhysAddr, VirtAddr}; pub mod instructions; diff --git a/src/structures/paging/mapper/recursive_page_table.rs b/src/structures/paging/mapper/recursive_page_table.rs index 5e14df0fc..ee05a2517 100644 --- a/src/structures/paging/mapper/recursive_page_table.rs +++ b/src/structures/paging/mapper/recursive_page_table.rs @@ -4,6 +4,7 @@ use super::*; use crate::registers::control::Cr3; +use crate::structures::paging::PageTableIndex; use crate::structures::paging::{ frame_alloc::FrameAllocator, page::NotGiantPageSize, @@ -11,7 +12,6 @@ use crate::structures::paging::{ Page, PageSize, PhysFrame, Size1GiB, Size2MiB, Size4KiB, }; use crate::VirtAddr; -use ux::u9; /// A recursive page table is a last level page table with an entry mapped to the table itself. /// @@ -29,7 +29,7 @@ use ux::u9; #[derive(Debug)] pub struct RecursivePageTable<'a> { p4: &'a mut PageTable, - recursive_index: u9, + recursive_index: PageTableIndex, } impl<'a> RecursivePageTable<'a> { @@ -67,7 +67,7 @@ impl<'a> RecursivePageTable<'a> { /// Creates a new RecursivePageTable without performing any checks. /// /// The `recursive_index` parameter must be the index of the recursively mapped entry. - pub unsafe fn new_unchecked(table: &'a mut PageTable, recursive_index: u9) -> Self { + pub unsafe fn new_unchecked(table: &'a mut PageTable, recursive_index: PageTableIndex) -> Self { RecursivePageTable { p4: table, recursive_index, @@ -581,11 +581,11 @@ impl<'a> MapperAllSizes for RecursivePageTable<'a> { } } -fn p3_ptr(page: Page, recursive_index: u9) -> *mut PageTable { +fn p3_ptr(page: Page, recursive_index: PageTableIndex) -> *mut PageTable { p3_page(page, recursive_index).start_address().as_mut_ptr() } -fn p3_page(page: Page, recursive_index: u9) -> Page { +fn p3_page(page: Page, recursive_index: PageTableIndex) -> Page { Page::from_page_table_indices( recursive_index, recursive_index, @@ -594,11 +594,11 @@ fn p3_page(page: Page, recursive_index: u9) -> Page { ) } -fn p2_ptr(page: Page, recursive_index: u9) -> *mut PageTable { +fn p2_ptr(page: Page, recursive_index: PageTableIndex) -> *mut PageTable { p2_page(page, recursive_index).start_address().as_mut_ptr() } -fn p2_page(page: Page, recursive_index: u9) -> Page { +fn p2_page(page: Page, recursive_index: PageTableIndex) -> Page { Page::from_page_table_indices( recursive_index, recursive_index, @@ -607,11 +607,11 @@ fn p2_page(page: Page, recursive_index: u9) -> Page { ) } -fn p1_ptr(page: Page, recursive_index: u9) -> *mut PageTable { +fn p1_ptr(page: Page, recursive_index: PageTableIndex) -> *mut PageTable { p1_page(page, recursive_index).start_address().as_mut_ptr() } -fn p1_page(page: Page, recursive_index: u9) -> Page { +fn p1_page(page: Page, recursive_index: PageTableIndex) -> Page { Page::from_page_table_indices( recursive_index, page.p4_index(), diff --git a/src/structures/paging/mod.rs b/src/structures/paging/mod.rs index a5a405ac6..c972cf803 100644 --- a/src/structures/paging/mod.rs +++ b/src/structures/paging/mod.rs @@ -11,7 +11,7 @@ pub use self::mapper::{Mapper, MapperAllSizes}; #[doc(no_inline)] pub use self::mapper::{OffsetPageTable, RecursivePageTable}; pub use self::page::{Page, PageSize, Size1GiB, Size2MiB, Size4KiB}; -pub use self::page_table::{PageTable, PageTableFlags}; +pub use self::page_table::{PageOffset, PageTable, PageTableFlags, PageTableIndex}; pub mod frame; mod frame_alloc; diff --git a/src/structures/paging/page.rs b/src/structures/paging/page.rs index beeca2476..08aa0edb7 100644 --- a/src/structures/paging/page.rs +++ b/src/structures/paging/page.rs @@ -1,10 +1,10 @@ //! Abstractions for default-sized and huge virtual memory pages. +use crate::structures::paging::PageTableIndex; use crate::VirtAddr; use core::fmt; use core::marker::PhantomData; use core::ops::{Add, AddAssign, Sub, SubAssign}; -use ux::*; /// Trait for abstracting over the three possible page sizes on x86_64, 4KiB, 2MiB, 1GiB. pub trait PageSize: Copy + Eq + PartialOrd + Ord { @@ -92,12 +92,12 @@ impl Page { } /// Returns the level 4 page table index of this page. - pub fn p4_index(&self) -> u9 { + pub fn p4_index(&self) -> PageTableIndex { self.start_address().p4_index() } /// Returns the level 3 page table index of this page. - pub fn p3_index(&self) -> u9 { + pub fn p3_index(&self) -> PageTableIndex { self.start_address().p3_index() } @@ -114,14 +114,17 @@ impl Page { impl Page { /// Returns the level 2 page table index of this page. - pub fn p2_index(&self) -> u9 { + pub fn p2_index(&self) -> PageTableIndex { self.start_address().p2_index() } } impl Page { /// Returns the 1GiB memory page with the specified page table indices. - pub fn from_page_table_indices_1gib(p4_index: u9, p3_index: u9) -> Self { + pub fn from_page_table_indices_1gib( + p4_index: PageTableIndex, + p3_index: PageTableIndex, + ) -> Self { use bit_field::BitField; let mut addr = 0; @@ -133,7 +136,11 @@ impl Page { impl Page { /// Returns the 2MiB memory page with the specified page table indices. - pub fn from_page_table_indices_2mib(p4_index: u9, p3_index: u9, p2_index: u9) -> Self { + pub fn from_page_table_indices_2mib( + p4_index: PageTableIndex, + p3_index: PageTableIndex, + p2_index: PageTableIndex, + ) -> Self { use bit_field::BitField; let mut addr = 0; @@ -146,7 +153,12 @@ impl Page { impl Page { /// Returns the 4KiB memory page with the specified page table indices. - pub fn from_page_table_indices(p4_index: u9, p3_index: u9, p2_index: u9, p1_index: u9) -> Self { + pub fn from_page_table_indices( + p4_index: PageTableIndex, + p3_index: PageTableIndex, + p2_index: PageTableIndex, + p1_index: PageTableIndex, + ) -> Self { use bit_field::BitField; let mut addr = 0; @@ -158,7 +170,7 @@ impl Page { } /// Returns the level 1 page table index of this page. - pub fn p1_index(&self) -> u9 { + pub fn p1_index(&self) -> PageTableIndex { self.start_address().p1_index() } } diff --git a/src/structures/paging/page_table.rs b/src/structures/paging/page_table.rs index b609aaa77..c3b39e303 100644 --- a/src/structures/paging/page_table.rs +++ b/src/structures/paging/page_table.rs @@ -7,7 +7,6 @@ use super::{PageSize, PhysFrame, Size4KiB}; use crate::addr::PhysAddr; use bitflags::bitflags; -use ux::*; /// The error returned by the `PageTableEntry::frame` method. #[derive(Debug, Clone, Copy, PartialEq)] @@ -217,16 +216,16 @@ impl IndexMut for PageTable { } } -impl Index for PageTable { +impl Index for PageTable { type Output = PageTableEntry; - fn index(&self, index: u9) -> &Self::Output { + fn index(&self, index: PageTableIndex) -> &Self::Output { &self.entries[cast::usize(u16::from(index))] } } -impl IndexMut for PageTable { - fn index_mut(&mut self, index: u9) -> &mut Self::Output { +impl IndexMut for PageTable { + fn index_mut(&mut self, index: PageTableIndex) -> &mut Self::Output { &mut self.entries[cast::usize(u16::from(index))] } } @@ -236,3 +235,59 @@ impl fmt::Debug for PageTable { self.entries[..].fmt(f) } } + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct PageTableIndex(u16); + +impl PageTableIndex { + pub fn new(index: u16) -> Self { + assert!(usize::from(index) < ENTRY_COUNT); + Self(index) + } +} + +impl From for u16 { + fn from(index: PageTableIndex) -> Self { + index.0 + } +} + +impl From for u32 { + fn from(index: PageTableIndex) -> Self { + u32::from(index.0) + } +} + +impl From for u64 { + fn from(index: PageTableIndex) -> Self { + u64::from(index.0) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct PageOffset(u16); + +impl PageOffset { + pub fn new(offset: u16) -> Self { + assert!(offset < (1 << 12)); + Self(offset) + } +} + +impl From for u16 { + fn from(offset: PageOffset) -> Self { + offset.0 + } +} + +impl From for u32 { + fn from(offset: PageOffset) -> Self { + u32::from(offset.0) + } +} + +impl From for u64 { + fn from(offset: PageOffset) -> Self { + u64::from(offset.0) + } +}