Skip to content

Commit

Permalink
Replace ux dependency with custom wrapper structs
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-opp committed Sep 25, 2019
1 parent 846198e commit 4e71da5
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 43 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
22 changes: 11 additions & 11 deletions src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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())
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/structures/paging/mapper/recursive_page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use super::*;
use crate::registers::control::Cr3;
use crate::structures::paging::PageTableIndex;
use crate::structures::paging::{
frame_alloc::FrameAllocator,
page::NotGiantPageSize,
page_table::{FrameError, PageTable, PageTableEntry, PageTableFlags},
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.
///
Expand All @@ -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> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -581,11 +581,11 @@ impl<'a> MapperAllSizes for RecursivePageTable<'a> {
}
}

fn p3_ptr<S: PageSize>(page: Page<S>, recursive_index: u9) -> *mut PageTable {
fn p3_ptr<S: PageSize>(page: Page<S>, recursive_index: PageTableIndex) -> *mut PageTable {
p3_page(page, recursive_index).start_address().as_mut_ptr()
}

fn p3_page<S: PageSize>(page: Page<S>, recursive_index: u9) -> Page {
fn p3_page<S: PageSize>(page: Page<S>, recursive_index: PageTableIndex) -> Page {
Page::from_page_table_indices(
recursive_index,
recursive_index,
Expand All @@ -594,11 +594,11 @@ fn p3_page<S: PageSize>(page: Page<S>, recursive_index: u9) -> Page {
)
}

fn p2_ptr<S: NotGiantPageSize>(page: Page<S>, recursive_index: u9) -> *mut PageTable {
fn p2_ptr<S: NotGiantPageSize>(page: Page<S>, recursive_index: PageTableIndex) -> *mut PageTable {
p2_page(page, recursive_index).start_address().as_mut_ptr()
}

fn p2_page<S: NotGiantPageSize>(page: Page<S>, recursive_index: u9) -> Page {
fn p2_page<S: NotGiantPageSize>(page: Page<S>, recursive_index: PageTableIndex) -> Page {
Page::from_page_table_indices(
recursive_index,
recursive_index,
Expand All @@ -607,11 +607,11 @@ fn p2_page<S: NotGiantPageSize>(page: Page<S>, recursive_index: u9) -> Page {
)
}

fn p1_ptr(page: Page<Size4KiB>, recursive_index: u9) -> *mut PageTable {
fn p1_ptr(page: Page<Size4KiB>, recursive_index: PageTableIndex) -> *mut PageTable {
p1_page(page, recursive_index).start_address().as_mut_ptr()
}

fn p1_page(page: Page<Size4KiB>, recursive_index: u9) -> Page {
fn p1_page(page: Page<Size4KiB>, recursive_index: PageTableIndex) -> Page {
Page::from_page_table_indices(
recursive_index,
page.p4_index(),
Expand Down
2 changes: 1 addition & 1 deletion src/structures/paging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 20 additions & 8 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -92,12 +92,12 @@ impl<S: PageSize> Page<S> {
}

/// 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()
}

Expand All @@ -114,14 +114,17 @@ impl<S: PageSize> Page<S> {

impl<S: NotGiantPageSize> Page<S> {
/// 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<Size1GiB> {
/// 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;
Expand All @@ -133,7 +136,11 @@ impl Page<Size1GiB> {

impl Page<Size2MiB> {
/// 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;
Expand All @@ -146,7 +153,12 @@ impl Page<Size2MiB> {

impl Page<Size4KiB> {
/// 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;
Expand All @@ -158,7 +170,7 @@ impl Page<Size4KiB> {
}

/// 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()
}
}
Expand Down
65 changes: 60 additions & 5 deletions src/structures/paging/page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -217,16 +216,16 @@ impl IndexMut<usize> for PageTable {
}
}

impl Index<u9> for PageTable {
impl Index<PageTableIndex> 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<u9> for PageTable {
fn index_mut(&mut self, index: u9) -> &mut Self::Output {
impl IndexMut<PageTableIndex> for PageTable {
fn index_mut(&mut self, index: PageTableIndex) -> &mut Self::Output {
&mut self.entries[cast::usize(u16::from(index))]
}
}
Expand All @@ -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<PageTableIndex> for u16 {
fn from(index: PageTableIndex) -> Self {
index.0
}
}

impl From<PageTableIndex> for u32 {
fn from(index: PageTableIndex) -> Self {
u32::from(index.0)
}
}

impl From<PageTableIndex> 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<PageOffset> for u16 {
fn from(offset: PageOffset) -> Self {
offset.0
}
}

impl From<PageOffset> for u32 {
fn from(offset: PageOffset) -> Self {
u32::from(offset.0)
}
}

impl From<PageOffset> for u64 {
fn from(offset: PageOffset) -> Self {
u64::from(offset.0)
}
}

0 comments on commit 4e71da5

Please sign in to comment.